小编Moo*_*oo3的帖子

window.location 是非阻塞操作吗?

在处理缺陷时,我遇到了 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

5
推荐指数
1
解决办法
2355
查看次数

标签 统计

javascript ×1