简体   繁体   English

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

[英]Check cookie and redirect user with jQuery?

I have a site that has a log in page for english and one for french.我有一个网站,有一个英语登录页面和一个法语登录页面。 I am checking for a cookie on page load and if it does not exist redirects user to the english log in page.我正在检查页面加载时的 cookie,如果它不存在,则将用户重定向到英文登录页面。 Is there a way to conditionally check if the user is trying to access a french page to redirect them to the french log in page?有没有办法有条件地检查用户是否正在尝试访问法语页面以将他们重定向到法语登录页面? Right now I have the logic setup but it creates a bad loop and does not work?现在我有逻辑设置,但它会创建一个坏循环并且不起作用? is this even possible with jquery? jquery 这甚至可能吗?

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);
  }

Your logic is saying that (assuming there is no x-access cookie), if we are not at /landing.html , go there.你的逻辑是说(假设没有x-access cookie),如果我们不在/landing.html , go 那里。 (One second, heading over...) and if you are not at /landing-fr.html (No I am NOT! You just sent me to /landing.html !) then go there ... (Ok, fine, I'll go. But I was there already and you sent me away...) (一秒钟,前进......)如果你不在/landing-fr.html (不,我不是!你刚刚把我送到/landing.html !)然后 Z34D1F91FB2E514B8576FAB1A7那里......我会 go。但我已经在那里了,你把我送走了......)

Can you see the loop you are making?你能看到你正在制作的循环吗?

Remember, every time a redirect happens, your code executes in the new page, having no recollection of ever having run before...请记住,每次重定向发生时,您的代码都会在新页面中执行,不记得曾经运行过......

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

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