[英]Add condition to cookie check function without causing infinite loop
我有一個 web 頁面,它在頁面加載時檢查 cookie,如果 cookie 不存在,則根據條件將用戶重定向到某個登錄頁面。 我正在嘗試向 cookie 檢查添加另一個條件,但它正在創建一個無限循環。 有沒有辦法允許另一個條件?
function checkCookie() {
var current_url = window.location.pathname;
var logInCookie = _siteNS.Utils.readCookie('x-access');
if (!logInCookie) {
if (
window.location.toString().includes('-fr') &&
current_url != '/landing-fr.html'
) {
window.location.replace('/landing-fr.html');
} else if (
window.location.href.indexOf('-fr') === -1 &&
current_url != '/landing.html'
) {
window.location.replace('/landing.html');
} else if (
window.location.href.indexOf('-fr') === -1 && *=== This is creating infinite loop===*
current_url === '/Important_Safety_Information.html'
) {
window.location.replace('/Important_Safety_Information.html');
}
}
}
問題是您將用戶重定向回他們已經在的頁面。 當用戶加載同一頁面時,它將再次調用相同的邏輯並再次將用戶重定向到同一頁面。
來自對上述問題的評論:
我想要做的是,如果用戶轉到“/Important_Safety_Information.html”,請留在那里並且不要重定向
如果您不想重定向用戶,那么...不要重定向用戶。 完全刪除最后一個else if
塊,因為您不想執行該操作。 或者,在最壞的情況下,如果您需要將此條件包含在正在進行的條件列表中,則只需將塊留空:
else if (
window.location.href.indexOf('-fr') === -1 &&
current_url === '/Important_Safety_Information.html'
) { /* do nothing */ }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.