[英]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.