javascript window.open 返回 null 并在内联脚本中失败,但可以从控制台运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18401331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:46:25  来源:igfitidea点击:

window.open returns null and fails in inline script but works from console

javascriptwindow.open

提问by Tomá? Zato - Reinstate Monica

I'm using Smarty template system. One of its features is posibility to output script that generates debug information for every page. Here you can see an example of generated code:

我正在使用 Smarty 模板系统。它的功能之一是可以输出为每个页面生成调试信息的脚本。在这里您可以看到生成代码的示例:

<script type="text/javascript">
//<![CDATA[

setTimeout(function() {  //Attempt to fix the issue with timeout
    var _smarty_console = window.open("about:blank","md5hash","width=680,height=600,resizable,scrollbars=yes");
    console.log(_smarty_console);  //Trying to log it
    if(_smarty_console!=null) {
      _smarty_console.document.write("<!DOCTY... lots of HTML ...<\/html>\n");
      _smarty_console.document.close();
    }
}, 5000);
//]]> 
</script>

The problem is, that the window.openfunctions always returns null. I tried to delay it with setTimeoutbut nothing changed. When I copy the code and run it in Firebug console, it works properly. There are no other scripts on page. The page uses strict XHTML. The script is right before </body>.

问题是,window.open函数总是返回null. 我试图推迟它,setTimeout但没有任何改变。当我复制代码并在 Firebug 控制台中运行它时,它可以正常工作。页面上没有其他脚本。该页面使用严格的 XHTML。脚本就在</body>.

回答by metadings

It is blocked by the browser. window.openis only not being blocked, when it is invoked by user action, for example in a click event, emitted by a native browser event. Also javaScript emitted events are being blocked, just like delayed setTimeout callbacks.

它被浏览器阻止。window.open只有当它被用户操作调用时才不会被阻止,例如在本地浏览器事件发出的点击事件中。javaScript 发出的事件也被阻止,就像延迟的 setTimeout 回调一样。

<a id="link" href="http://stackoverflow.com">StackOverflow</a>

<script type="text/javascript">

// Example (with jQuery for simplicity)

$("a#link").click(function (e) {
  e.preventDefault();

  var url = this.href;

  // this will not be blocked
  var w0 = window.open(url);
  console.log("w0: " + !!w0); // w0: true

  window.setTimeout(function () {
    // this will be blocked
    var w1 = window.open(url);
    console.log("w1: " + !!w1); // w1: false
  }, 5000);
});

</script>

Watch the Fiddle. I also tried it with the keypressevent, but no luck.

观看小提琴。我也在这个keypress事件中尝试过,但没有运气。

window.openreturns a valid reference to the new (or an existing named) window, or nullwhen it failed to create a new window.

window.open返回对新(或现有命名)窗口的有效引用,或者null当它无法创建新窗口时。

回答by Zsolt

Try the next command after window.open with timeout, for example:

尝试在 window.open 后超时的下一个命令,例如:

var myWindow = window.open('foo','_blank','menubar=no, scrollbars=yes, top=10, width=800,height=600');

setTimeout( myWindow.onload=function(){this.document.body.innerHTML+='bar';}, 2000 );