window.open()
在 iOS 网络应用程序中调用时,页面会在网络应用程序中打开,而不是在移动 safari 中打开。
如何强制网页在 mobile safari 中打开?
注意:使用直线<a href>
链接不是一种选择。
window.open()
在 iOS 网络应用程序中调用时,页面会在网络应用程序中打开,而不是在移动 safari 中打开。
如何强制网页在 mobile safari 中打开?
注意:使用直线<a href>
链接不是一种选择。
这个有可能。使用 iOS5 独立 Web 应用程序测试:
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的Web应用程序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>
您可以使用 window.open() 强制 iOS 在 safari 中打开来自 PWA 的链接,前提是您使用不同的子域或使用 http 而不是 https。
例如以example.com为例
window.open('http://example.com','_blank')
注意:在浏览器中打开后,我的服务器会将 http 重定向到 https。所以没有安全问题。
(或者)
window.open('api.example.com','_blank')