繁体   English   中英

检查 cookie 并使用 jQuery 重定向用户?

[英]Check cookie and redirect user with jQuery?

我有一个网站,有一个英语登录页面和一个法语登录页面。 我正在检查页面加载时的 cookie,如果它不存在,则将用户重定向到英文登录页面。 有没有办法有条件地检查用户是否正在尝试访问法语页面以将他们重定向到法语登录页面? 现在我有逻辑设置,但它会创建一个坏循环并且不起作用? jquery 这甚至可能吗?

jQuery:

function checkCookie() {
    var current_url = window.location.pathname;
    var logInCookie = _siteNS.Utils.readCookie('x-access');
   
    if (!logInCookie && current_url != '/landing.html') {
      window.location.replace('/landing.html');

    } else if (!logInCookie && current_url != '/landing-fr.html') {
      window.location.replace('/landing-fr.html');
    }
    console.log('cookie', logInCookie);
  }

你的逻辑是说(假设没有x-access cookie),如果我们不在/landing.html , go 那里。 (一秒钟,前进......)如果你不在/landing-fr.html (不,我不是!你刚刚把我送到/landing.html !)然后 Z34D1F91FB2E514B8576FAB1A7那里......我会 go。但我已经在那里了,你把我送走了......)

你能看到你正在制作的循环吗?

请记住,每次重定向发生时,您的代码都会在新页面中执行,不记得曾经运行过......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM