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
 |  Show 2 more ments

1 Answer 1

Reset to default 3

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>

本文标签: javascriptwindowopen() returns undefined when called from setTimeout on AndroidStack Overflow