iOS Safari - 将焦点放在窗口上

Ale*_*lex 5 html javascript jquery mobile-safari ios

我有这个jsFiddle,它有一个简单div的 jQuery 点击处理程序:

<div class="testDiv" 
   data-target="http://www.stackoverflow.com" 
   data-open="false">
</div>

$(document).ready(function(){
    var newWindow;
    $('.testDiv').on('click', function(e){
        var target = $(this).data('target');
        var open = $(this).data('open');

        if (!open){
            $(this).data('open','true');
            newWindow = window.open(target);
        }
        else {
            if (newWindow.closed){
                newWindow = window.open(target);
            }
            newWindow.focus();
        }
    });
})
Run Code Online (Sandbox Code Playgroud)

上的点击处理程序 div旨在打开一个尚未打开的新窗口;如果它是打开的,它会给那个窗口焦点。这在 Win 7 x64 上的 Chrome 中运行良好,但在 iOS 8.3 的 Safari(在 iPhone 6 上)上,一旦窗口打开,单击div不会将焦点切换到打开的窗口。您必须关闭窗口并再次打开它才能获得焦点。

这个 iOS Safari 焦点问题的任何解决方案?

Fel*_*lix 1

这对我有用,在 iOS(平板电脑)、MacOS (M1)、Chrome 上进行了测试:

const link = 'http://...';
const target = 'NEW_WINDOW'

let win = window.open(link, target);

if (win && !win.closed) {
  win.close();
}

win = window.open(link, target);

if (win) {
  setTimeout(() => win.focus(), 100);
}
Run Code Online (Sandbox Code Playgroud)