【问题标题】:window.open() returns undefined when called from setTimeout on Android在 Android 上从 setTimeout 调用时 window.open() 返回 undefined
【发布时间】:2010-08-30 14:18:41
【问题描述】:

我的 Android 模拟器出现异常行为。当从 setTimeout 或回调函数调用时 window.open() 总是返回 undefined,例如AJAX 回调。但是 window.open() 从事件处理程序调用时成功打开一个弹出窗口,例如点击 这是示例代码:

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

在示例中 alert(window.open('about:blank')) 显示 'undefined' 并且未创建弹出窗口 当直接从 fnc() 调用时,同样的函数也起作用

有什么想法吗?

谢谢

【问题讨论】:

  • 我怀疑这只是普通的弹出窗口阻止行为。
  • 我在 Xul 应用程序中遇到了同样的 javascript 问题,所以它不是 Android 的问题。我建议你删除 Android 标签。
  • @Pointy 不是,在桌面应用程序中出现同样的问题(使用 Xul)
  • @Andrey 你还在用这个账号吗?
  • @Tom Brito 在 web 应用程序中,你肯定无法从超时处理程序中打开带有window.open() 的窗口。

标签: javascript android


【解决方案1】:

尝试以下方法:

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

【讨论】:

  • 你的意思是return window.open('about:blank');go()里面?
  • 以上代码适用于我在 android 2.2 设备上 - 请注意,这有点像 hack...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
相关资源
最近更新 更多