javascript iPad Safari IOS 5 window.close() 关闭错误的窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7987625/
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-10-26 01:55:37  来源:igfitidea点击:

iPad Safari IOS 5 window.close() closing wrong window

javascriptipadmobile-safari

提问by Lissy

We have an iPad application that's working on our older iPads.

我们有一个 iPad 应用程序可以在我们的旧 iPad 上运行。

We open external links using var x = window.open(url)

我们使用 var x = window.open(url) 打开外部链接

at the end of the day, when the user closes this part of the app, we go through all the windows it opened and do x.close() for each one and everything is okie dokie.

在一天结束时,当用户关闭应用程序的这一部分时,我们会浏览它打开的所有窗口并为每个窗口执行 x.close() ,一切都很好。

Testing on the new iPad with IOS 5 and the lovely tabs, opening the new windows (although now they open as tabs) is fine, but doing x.close() doesn't seem to necessarily close x, it may close window y or z. Doing x.focus() or y.focus() works just fine, the correct tab comes into focus, but close seems to just pick whatever tab it wants.

在带有 IOS 5 和可爱标签的新 iPad 上进行测试,打开新窗口(虽然现在它们作为标签打开)很好,但是执行 x.close() 似乎不一定关闭 x,它可能会关闭窗口 y 或z。做 x.focus() 或 y.focus() 工作得很好,正确的选项卡成为焦点,但 close 似乎只是选择它想要的任何选项卡。

Is this a bug or am I doing something wrong? Example program:

这是一个错误还是我做错了什么?示例程序:

<html>
<head></head>
<body>
    <script>
        //The openWindow array will hold the handles of all open child windows
        var openWindow = new Array();
       var win1;
       var win2;
        //Track open adds the new child window handle to the array.
        function trackOpen(winName) {
            openWindow[openWindow.length]=winName;
        }

        //loop over all known child windows and try to close them.  No error is
        //thrown if a child window(s) was already closed.
        function closeWindows() {
            var openCount = openWindow.length;
            for(r=openCount-1;r>=0;r--) {
                openWindow[r].close();
            }
        }

        //Open a new child window and add it to the tracker.
        function open1() {
            win1 = window.open("http://www.yahoo.com");
            trackOpen(win1);
        }

        //Open a different child window and add it to the tracker.
        function open2() {
            win2 = window.open("http://www.google.com");
            trackOpen(win2);

        }
        //Open whatever the user enters and add it to the tracker
        function open3() {
            var newURL = document.getElementById("url").value;
            var win3= window.open(newURL);
            trackOpen(win3);
        }

    </script>
    <input type="button" value="Open 1" onclick="open1()">
    <input type="button" value="Open 2" onclick="open2()">
    <input type="button" value="Focus 1" onclick="win1.focus()">
    <input type="button" value="Focus 2" onclick="win2.focus()">
    <input type="button" value="Close 1" onclick="win1.close()">
    <input type="button" value="Close 2" onclick="win2.close()">

    URL: <input type="text" id="url"> <input type="button" value="Open URL" onclick="open3()">
    <input type="button" value="Close All" onclick="closeWindows()">

</body>
</html>

回答by Cee McSharpface

That did the trick for me (iPad 2 and 3; 3 with iOS 5.1.1)

这对我有用(iPad 2 和 3;iOS 5.1.1 的 3)

var host=window.opener;
window.focus(); /* solves the iPad3 problem */
window.close(); /* the actual closing we want to achieve... */
/* makes the focus go back to opener on iPad2, fails silently on iPad3 */
try { host.focus(); } catch(e) {} 

回答by pronouncedJerry

Set focus on the window before closing it

在关闭窗口之前将焦点设置在窗口上

回答by JohnP

I had the same issue as well. My initial code would focus on the parent window and then close the current one. This always ends up closing the wrong window. What I did was to remove the .focuscall and added a small delay to the window.close() and it worked for me. Original source for the delay suggestion - https://twitter.com/#!/gryzzly/statuses/177061204114685952

我也有同样的问题。我的初始代码将重点放在父窗口上,然后关闭当前窗口。这总是最终关闭错误的窗口。我所做的是删除.focus调用并向 window.close() 添加一个小的延迟,它对我有用。延迟建议的原始来源 - https://twitter.com/#!/gryzzly/statuses/177061204114685952