在 iOS 的 web 应用中,调用 window.open()
方法时,页面会在 web 应用中打开,而不是在移动 Safari 中打开。
我该如何强制让网页在移动 Safari 中打开?
注意:使用直接的 <a href>
链接不是一个选项。
在 iOS 的 web 应用中,调用 window.open()
方法时,页面会在 web 应用中打开,而不是在移动 Safari 中打开。
我该如何强制让网页在移动 Safari 中打开?
注意:使用直接的 <a href>
链接不是一个选项。
<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);
<div id="foz" data-href="http://www.google.fi">Google</div>
吗?用<div>
开头,然后用</a>
结尾并不合理。而且最后的</li>
是什么意思? - Timo Ernst原来使用 JavaScript 的 window.open()
无法在 iOS web 应用程序中打开链接,如果你想在移动 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。
例如,对于take example.com
window.open('http://example.com','_blank')
window.open('api.example.com','_blank')