简体   繁体   English

Javascript函数仅在有正常重定向警报时才有效

[英]Javascript function only works if there's an alert for normal redirection

Below is code which is use to redirect the URL and not working on Mozilla Browser 下面是用于重定向URL而不是在Mozilla浏览器上工作的代码

#Working Code#
alert("HI");
var logouturl = getCookie("ch_logout_url");
location.replace(logouturl);

#Not Working Code#
var logouturl = getCookie("ch_logout_url");
location.replace(logouturl);

You can wrap your code in: 您可以将代码包装在:

document.addEventListener('DOMContentLoaded', function() {
  // ...
}); //Vanilla Js Alternative to jquery(document).ready();

or 要么

(function() {
  // ... your code
})(); //Immediately Invoked Function Expression

Then your code will run only when the page is already loaded, not absolutely true for the second example but will still work. 然后你的代码只在页面已经加载时才会运行,对于第二个例子来说不是绝对正确的,但仍然有效。

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

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