我想根据Cookie信息重定向访问者。 例如,在这种情况下:访客访问该网站,将向他展示模态,询问其邮政编码,假设他使用33146作为邮政编码,这是迈阿密邮政编码,它将邮政编码保存在cookie中,并重定向到/集合/迈阿密/
尽管重定向前一切正常,但我尝试做这样的事情,将邮政编码保存到cookie。我有一个刷新循环,即它首先重定向到/ collections / miami并继续执行直到地址栏类似于:/ collections / miami / collections / miami / collections / miami / collections / miami /这样的结果无休止的循环。
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
$(window).on('load',function(){
if (document.cookie.indexOf('visited=true') == -1){
$('#openModal').dialog({modal : true});
var year = 1000*60*60*24*365;
var expires = new Date((new Date()).valueOf() + year);
document.cookie = "visited=true;expires=" + expires.toUTCString();
}else{
var zip_code = getCookie('zip_code');
let LA = zipcodesLosAngelesUSA.includes(Number(zip_code))
if(LA){
var link = location.protocol + '//' + location.host + location.pathname + 'los-angeles'
document.location.href = link
}else{
invalid.style.display = 'block'
}
let ARIZONA = zipcodesArizonaUSA.includes(Number(zip_code))
if(ARIZONA){
var link = location.protocol + '//' + location.host + location.pathname + 'arizona'
document.location.href = link
}else{
invalid.style.display = 'block'
}
let TORONTO = zipcodesTorontoCAD.includes(String(zip_code))
if(TORONTO){
var link = location.protocol + '//' + location.host + location.pathname + 'toronto'
document.location.href = link
}else{
invalid.style.display = 'block';
}
let MIAMI = zipcodesMiamiUSA.includes(Number(zip_code))
if(MIAMI){
var link = location.protocol + '//' + location.host + location.pathname + '/collections/miami/'
document.location.href = link
}else{
invalid.style.display = 'block';
}
let NYC = zipcodesNycUSA.includes(Number(zip_code))
if(NYC){
var link = location.protocol + '//' + location.host + location.pathname + 'nyc'
document.location.href = link
}else{
invalid.style.display = 'block';
}
}
submit.addEventListener("click", function(){
let LA = zipcodesLosAngelesUSA.includes(Number(input.value))
if(LA){
var year = 1000*60*60*24*365;
var expires = new Date((new Date()).valueOf() + year);
document.cookie = "zip_code="+Number(input.value)+";expires=" + expires.toUTCString();
var link = location.protocol + '//' + location.host + location.pathname + 'los-angeles'
document.location.href = link
}else{
invalid.style.display = 'block'
}
let ARIZONA = zipcodesArizonaUSA.includes(Number(input.value))
if(ARIZONA){
var year = 1000*60*60*24*365;
var expires = new Date((new Date()).valueOf() + year);
document.cookie = "zip_code="+Number(input.value)+";expires=" + expires.toUTCString();
var link = location.protocol + '//' + location.host + location.pathname + 'arizona'
document.location.href = link
}else{
invalid.style.display = 'block'
}
let TORONTO = zipcodesTorontoCAD.includes(String(input.value))
if(TORONTO){
var year = 1000*60*60*24*365;
var expires = new Date((new Date()).valueOf() + year);
document.cookie = "zip_code="+String(input.value)+";expires=" + expires.toUTCString();
var link = location.protocol + '//' + location.host + location.pathname + 'toronto'
document.location.href = link
}else{
invalid.style.display = 'block';
}
let MIAMI = zipcodesMiamiUSA.includes(Number(input.value))
if(MIAMI){
var year = 1000*60*60*24*365;
var expires = new Date((new Date()).valueOf() + year);
document.cookie = "zip_code="+Number(input.value)+";expires=" + expires.toUTCString();
var link = location.protocol + '//' + location.host + location.pathname + 'miami'
document.location.href = link
}else{
invalid.style.display = 'block';
}
let NYC = zipcodesNycUSA.includes(Number(input.value))
if(NYC){
var year = 1000*60*60*24*365;
var expires = new Date((new Date()).valueOf() + year);
document.cookie = "zip_code="+Number(input.value)+";expires=" + expires.toUTCString();
var link = location.protocol + '//' + location.host + location.pathname + 'nyc'
document.location.href = link
}else{
invalid.style.display = 'block';
}
})
有人可以帮助我修复这个愚蠢的错误并启发我做错了什么地方吗?