在 iOS Web 应用程序中调用window.open()
时,页面会在 Web 应用程序而不是移动 Safari 中打开。
如何强制网页在移动 Safari 中打开?
注意:<a href>
不能使用直接链接。
在 iOS Web 应用程序中调用window.open()
时,页面会在 Web 应用程序而不是移动 Safari 中打开。
如何强制网页在移动 Safari 中打开?
注意:<a href>
不能使用直接链接。
这个有可能。使用 iOS5 独立网络应用测试:
HTML:
<div id="foz" data-href="http://www.google.fi">Google</div>
JavaScript:
document.getElementById("foz").addEventListener("click", function(evt) {
var a = document.createElement('a');
a.setAttribute("href", this.getAttribute("data-href"));
a.setAttribute("target", "_blank");
var dispatch = document.createEvent("HTMLEvents");
dispatch.initEvent("click", true, true);
a.dispatchEvent(dispatch);
}, false);
事实证明,使用 JavaScript转义iOS 网络应用程序是不可能的window.open()
。如果您想在移动 Safari 中打开链接,则必须使用<a href>
链接。
Vue 实现。希望会有所帮助。
<template>
<div @click='handler()'>{{ text }}</div>
</template>
<script>
export default {
props: {
href: String,
text: String,
},
methods: {
handler() {
const a = document.createElement('a')
a.setAttribute('href', this.href)
a.dispatchEvent(new MouseEvent("click", {'view': window, 'bubbles': true, 'cancelable': true}))
}
}
}
</script>
如果您使用不同的子域或使用 http 而不是 https,则可以强制 iOS 在 safari 中使用 window.open() 打开来自 PWA 的链接。
以 example.com 为例
window.open('http://example.com','_blank')
注意:一旦在浏览器中打开,我的服务器会将 http 重定向到 https。所以没有安全问题。
(或者)
window.open('api.example.com','_blank')