views:

29

answers:

1

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

A: 

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>
Dror