在处理缺陷时,我遇到了 window.location 行为,这与我的理解不同。在此问题之前,我认为为window.location赋值会阻止其他操作并继续重定向。
// Try to answer the questions before you run this script on JSFiddle.
// 1. Which site is going to load as a result of redirect method - apple.com or mashable.com?
// 2. Will the console log 'script added' print in console?
function redirect(url) {
var el = document.createElement('script');
var head = document.getElementsByTagName("head")[0];
var content = "window.location.href='"+url+"'";
el.innerHTML = content;
head.appendChild(el);
}
console.log('adding scripts');
redirect('http://apple.com');
redirect('http://mashable.com');
console.log('script added');
Run Code Online (Sandbox Code Playgroud)
我试图寻找解释此行为的文档,但找不到任何文档。任何详细解释 window.location 的文档或文章都会很有用。
在Javascript中,设置window.location.href一般是非阻塞操作。
当 DNS 解析新页面时,您仍然可以运行 Javascript 操作。因此,如果您location.href在完成之前进行更改,那么您将重新开始该过程。
您可以在 WHATWG:Living HTML - HTML Standard 中阅读 Javascript 的预期执行方式window.location(具体而言) 。window.location.assign
window.location设置对象后,Web 浏览器将启动Location-object navigate或 子Location-object-setter navigate例程。
| 归档时间: |
|
| 查看次数: |
2355 次 |
| 最近记录: |