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

标签 javascript ipad mobile-safari

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

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

在一天结束时,当用户关闭应用程序的这一部分时,我们遍历它打开的所有窗口并对每个窗口执行 x.close(),一切正常。

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

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

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

最佳答案

这对我有用(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) {} 

关于javascript - iPad Safari IOS 5 window.close() 关闭错误窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7987625/

相关文章:

ios - 'NSInvalidUnarchiveOperationException',原因 : 'Could not instantiate class named UIStoryboardPresentationSegueTemplate'

ios - iPad 应用程序——viewDidLoad 触发前的长黑屏

ios - UIWebView 中的自定义按钮(适用于 iPad)

iphone - 如何从移动 safari 上的文本输入中删除边框样式?

jQuery 验证插件无法滚动到 iOS 8 Safari 中出现验证错误的字段

javascript - Typescript 获取函数内设置变量的名称

javascript - 查询 : How to loop Array disableTimeRanges from json

javascript - IE11 和 IFrame 历史对象

javascript - 为多选选择的 Angular

javascript - iOS Safari 中的 e.keyCode on keyup 事件