admin管理员组文章数量:1420530
I am having strange behaviour on my Android emulator. window.open() always returns undefined when called from setTimeout or callback function e.g. AJAX callback. However window.open() successfully opens a popup when called from an event handler e.g. onclick here is sample code:
<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>
I am having strange behaviour on my Android emulator. window.open() always returns undefined when called from setTimeout or callback function e.g. AJAX callback. However window.open() successfully opens a popup when called from an event handler e.g. onclick here is sample code:
<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()
Any ideas?
Thanks
Share Improve this question edited Dec 5, 2015 at 19:36 Solomon Ucko 6,1503 gold badges28 silver badges48 bronze badges asked Aug 30, 2010 at 14:18 AndreyAndrey 511 silver badge2 bronze badges 7- I suspect it's just ordinary popup-blocking behavior. – Pointy Commented Aug 30, 2010 at 14:19
- I'm having this same problem with javascript in a Xul app, so its not Android's problem. I suggest you remove the Android tag. – Student Commented Jan 11, 2011 at 18:34
- @Pointy it's not, same problem here in a desktop app (using Xul) – Student Commented Jan 11, 2011 at 19:45
- @Andrey are you still using this account? – Student Commented Jan 11, 2011 at 20:00
-
@Tom Brito well in a web application it's definitely the case that you cannot open a window with
window.open()
from a timeout handler. – Pointy Commented Jan 11, 2011 at 20:36
1 Answer
Reset to default 3Try 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>
本文标签: javascriptwindowopen() returns undefined when called from setTimeout on AndroidStack Overflow
版权声明:本文标题:javascript - window.open() returns undefined when called from setTimeout on Android - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745336122a2654047.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论