【问题标题】:window.close() doesn't work on iOSwindow.close() 在 iOS 上不起作用
【发布时间】:2012-05-29 14:03:48
【问题描述】:

我使用 window.open() 打开一个新窗口,将用户重定向到 oauth 登录页面。但是,在成功登录后,当用户被重定向回我的应用程序时,带有 window.open 调用的前一个窗口不会在 ios 中自行关闭。

在 iPad 上它会关闭错误的窗口,而在 iPhone 上它根本不会关闭窗口。该代码在 Android 和桌面版本的 Chrome 和 Firefox 上运行良好。

经过大量的根深蒂固,我找到了一个修复方法(发布在下面)。如果有人有更好的想法或根本原因,请在此处发布。

【问题讨论】:

    标签: javascript ios iphone ipad chrome-ios


    【解决方案1】:

    经过一番搜索,我发现这条推文发布了解决方法 - @gryzzly 的https://twitter.com/#!/gryzzly/statuses/177061204114685952

    全部复制到这里

    window.close() 在 window.open() 之后在 iOS 上不起作用或 目标=“_空白”?做 setTimeout(window.close, timeout);哪里超时> 300.

    这与删除 .focus() 一起,我在关闭新窗口之前专注于父窗口,完全解决了我的问题。

    【讨论】:

    • 在这里发推文!更好的建议是实际使用window.addEventListener("load", window.close);
    • 我已经离开了那个项目,所以我不确定我是否真的尝试过“加载”,但这是有道理的。将其归档以备将来使用,谢谢!
    【解决方案2】:

    这就是我最终开始工作的...
    永远无法让 window.close 函数工作;即使在如上所示的 setTimeout 中

    我对此进行了测试:
    windows XP : Chrome20,Firefox12,IE8
    安卓姜饼:安卓浏览器
    Android Ice Cream : android 浏览器, Firefox
    Ipad:默认浏览器(我假设它是 safari)
    Iphone 3gs 和 4s:默认


    <SCRIPT LANGUAGE=\"JavaScript\">
        function refresh() {
            var sURL = unescape("http://(some web page)/");
            window.location.replace(sURL);
        }
        function closeWindow() {
            var isiPad = navigator.userAgent.match(/iPad/i) != null;
            var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
            if (isiPad || isiPhone) {
               setTimeout( \"refresh()\", 300 );
            } else {
               window.close();
            }
        }
    </SCRIPT>
    

    ......以及html代码......

    <p><input class="bigbutton" type="button" name="cancel" id="cancel" value="Cancel" onClick="closeWindow()"></p>
    

    【讨论】:

      【解决方案3】:

      ios 12、13 正常

      <p>If the page stops. Please press the button below to continue.</p>
      <input id="button" name="button" type="submit" value="Close">
      
      <script>
        var ua = window.navigator.userAgent;
      
        if (ua.indexOf('iPhone ') > 0) {  
          document.querySelector('#button').addEventListener('click', function (event) {
            event.preventDefault();
            window.close();
          }, false);
      
          document.querySelector('#button').click();
        } else {
          window.close();
        }
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-02
        相关资源
        最近更新 更多