简体   繁体   中英

Window redirect to specific location after alert

I have following javascript code which is used to redirect the user to specific location after five seconds.

In Chrome Browser if I run this code keeping the tab active it shows the alert pop up and redirect to the specific location, If tab is not active it will directly go to specific location without alert.

The same is not happening in Firefox if tab is active or not it always show the alert before redirect. Is there a way we can do the same for firefox (similar to what chrome does).

setTimeout(function(){ 
alert("you are redirected"); window.location="https://jsfiddle.net/user/login/" 
}, 5000);

I'm not sure but maybe document.hidden can be used for this:

setTimeout(function(){ 
    if(!document.hidden){alert("you are redirected");} window.location="https://jsfiddle.net/user/login/" 
    }, 5000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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