[英]$window.focus() doesn't work on existing tabs in iOS Safari
[英]iOS Safari - Give focus to window
我有这个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();
}
});
})
div
上的点击处理程序旨在打开一个新的 window(如果它尚未打开); 如果它是打开的,它会给 window 焦点。 This works fine in Chrome on Win 7 x64 but on iOS 8.3's Safari (on an iPhone 6), once the window is open, clicking the div
doesn't switch focus to the opened window. 您必须关闭 window 并再次打开它才能获得焦点。
此 iOS Safari 焦点问题的任何解决方案?
这对我有用,在 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.