Ste*_*eve 46 javascript safari macos
我已经构建了一个网页,让您从下拉列表中选择一个页面名称,然后将浏览器传输到该页面.执行传输的代码是
if (url){
window.open(url, '_blank');
}
Run Code Online (Sandbox Code Playgroud)
其中"url"是选中的页面.
window.open行之前的控制台日志打印如下:
executing: window.open(http://www.mywebsite.com/44/threats.html, '_blank')
Run Code Online (Sandbox Code Playgroud)
然后浏览器在新选项卡中打开页面.
这适用于所有浏览器的Windows 7,包括Safari.
在iMac上,它适用于Firefox但不适用于Safari.
有谁知道为什么iMac/Safari不会这样做?
小智 123
Safari阻止对在异步调用中进行的window.open()的任何调用.
我发现这个问题的解决方案是在进行asnyc调用之前调用window.open并在promise解析时设置位置.
var windowReference = window.open();
myService.getUrl().then(function(url) {
windowReference.location = url;
});
Run Code Online (Sandbox Code Playgroud)
Mig*_*ens 58
编辑:有些人报告说此方法在最新的 Safari 上不再适用。
window.open(url, '_blank')
使用 setTimeout 将代码行包装在异步函数中也可以,
setTimeout(() => {
window.open(url, '_blank');
})
Run Code Online (Sandbox Code Playgroud)
setTimeout 代码在主线程上运行,而不是在异步线程上运行。在 Chrome 和 Safari 中测试。
use*_*238 18
要在safari中使用window.open(),必须将其放在元素的onclick事件属性中.
例如:
<button class='btn' onclick='window.open("https://www.google.com", "_blank");'>Open Google search</button>
jnr*_*orp 17
取自史蒂夫于2013年12月20日接受的答案评论:
实际上,有一种非常简单的方法:只需在iMac/Safari浏览器中单击"阻止弹出窗口",它就可以实现我的目的.
为了澄清,在Mac OS X El Capitan上运行Safari时:
Abr*_*ham 10
您不能依赖,window.open
因为浏览器可能有不同的策略。我有同样的问题,我改用下面的代码。
let a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = <your_url>;
a.download = <your_fileName>;
a.click();
document.body.removeChild(a);
Run Code Online (Sandbox Code Playgroud)
小智 7
window.location.assign(url)
这window.open(url)
解决了 ios 设备中的问题
使用 JavaScript 以编程方式在新选项卡中打开链接:safari、mobile safari 和其他浏览器:
const link = 'https://google.com';
const a = document.createElement("a");
a.setAttribute('href', link);
a.setAttribute('target', '_blank');
a.click();
Run Code Online (Sandbox Code Playgroud)
Safari 中的“选项卡”下有一个设置,标Open pages in tabs instead of windows:
有带有几个选项的下拉菜单。我想你的可能会被设置为Always
. 最重要的是,您不能依赖浏览器打开新窗口。
归档时间: |
|
查看次数: |
74234 次 |
最近记录: |