如果满足条件,我正在使用window.location.href
将用户重定向到某个页面。我只把window.location.href
放在一个if
块里,没有一个else
块。当我执行代码时,虽然重定向有效,但if
块之后的代码也会被执行。页面代码的执行不应该在重定向到其他页面后立即停止吗?
如果 window.location.href 之后的 (if 条件) 有效,它将作为 window.location.href 执行,因为它需要一些时间才能重定向到另一个页面。
使用 abort
在 if
语句之后停止脚本。
喜欢这个:
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
..
写 exit(); 在 window.location.href = 'abc.com'之后;..这将重定向页面,并且紧随window.location.href之后的代码将不会执行。