Javascript window.close() 在 iOS 上不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10712906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:37:47  来源:igfitidea点击:

window.close() doesn't work on iOS

javascriptiosiphoneipadchrome-ios

提问by JohnP

I open a new window with window.open() to redirect users to the oauth login page. However, after successful login when the user is redirected back to my app the previous window with the window.open call does not close itself in ios.

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

On the iPad it would close the wrong window and on the iPhone it wouldn't close the window at all. The code works fine on Android and on desktop versions of Chrome and Firefox.

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

After much rooting about, I found a fix (posted below). If anyone has any better ideas or root causes, please post here.

经过多次扎根,我找到了一个修复程序(在下面发布)。如果有人有更好的想法或根本原因,请在此处发布。

采纳答案by JohnP

After some searching, I found this tweet which posts a workaround - https://twitter.com/#!/gryzzly/statuses/177061204114685952by @gryzzly

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

Copied here in full

完整复制到这里

window.close() doesn't work on iOS after window.open()ing or target="_blank"? do setTimeout(window.close, timeout); where timeout > 300.

window.close() 在 window.open()ing 或 target="_blank" 之后在 iOS 上不起作用?做 setTimeout(window.close, timeout); 其中超时 > 300。

This along with removing a .focus()in which I focus on the parent window before closing the new window completely solved the problem for me.

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

回答by Phil Adams

here is what I ended up getting to work...
never could get the window.close function to work; even in the setTimeout as shown above

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

I tested this on:
    windows XP : Chrome20,Firefox12,IE8
    Android gingerbread : android browser
    Android Ice Cream : android browser, Firefox
    Ipad : default browser (i assume its safari)
    Iphone 3gs and 4s : default

我在以下平台上进行了测试:
    Windows XP:Chrome20、Firefox12、IE8
    Android 姜饼:Android 浏览器
    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>

...... and the html code .......

......和html代码......

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


回答by Sheng Kung Chien

ios 12、13 OK

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>