简体   繁体   中英

Redirect using:window.location.href

I'm using window.location.href to redirect a user to some page if a condition is met. I've placed window.location.href inside an if block only, without an else block for it. When I execute my code, although the redirection works, but the code just after the if block also gets executed. Shouldn't the execution of the code of the page halt as soon as it is redirected to some other page?

如果 window.location.href 之后的(if 条件)有效,它将被执行,因为 window.location.href 需要一些时间来重定向到另一个页面。

Use abort to stop the script after the if statement.

Like this:

if(condition) {
   window.location.href = 'somewhere.com';
   abort; // it will stop the code below the if statement, IF the condition is true.
} 
..
some other code
..

Write exit(); after the window.location.href = 'abc.com';..This will redirect the page and the code just after window.location.href will not execute.

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