简体   繁体   English

window.open()在Android上从setTimeout调用时返回undefined

[英]window.open() returns undefined when called from setTimeout on Android

I am having strange behaviour on my Android emulator. 我的Android模拟器上有奇怪的行为。 window.open() always returns undefined when called from setTimeout or callback function eg AJAX callback. 当从setTimeout或回调函数调用时,window.open()总是返回undefined,例如AJAX回调。 However window.open() successfully opens a popup when called from an event handler eg onclick here is sample code: 但是,当从事件处理程序调用时,window.open()会成功打开一个弹出窗口,例如onclick这里是示例代码:

 <html> <head> </head> <body> <script type="text/javascript"> function fnc() { setTimeout(function() { alert(window.open('about:blank')) }, 100); } </script> <input type="button" onclick="fnc()" value="push me"> </body> </html> 

In the example alert(window.open('about:blank')) shows 'undefined' and the popup is not created The same function works when called straight from fnc() 在示例警报(window.open('about:blank'))中显示'undefined'并且未创建弹出窗口从fnc()直接调用时,同样的函数有效

Any ideas? 有任何想法吗?

Thanks 谢谢

Try the following: 请尝试以下方法:

 <html> <head> <script type="text/javascript"> function go(){ window.open('about:blank'); } function fnc() { var buttonnode= document.createElement('input'); buttonnode.setAttribute('type','button'); buttonnode.setAttribute('name','sal'); buttonnode.setAttribute('style','display:none;'); document.body.appendChild(buttonnode); buttonnode.onclick = go; setTimeout(function() { buttonnode.click() }, 100); } </script> </head> <body> <input type="button" onclick="fnc()" value="do later"><br/> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM