濟(jì)南網(wǎng)站seoseo黑帽技術(shù)工具
uniapp h5項(xiàng)目頁(yè)面中使用了iframe導(dǎo)致瀏覽器返回按鍵無(wú)法使用, 返回不了上一頁(yè).
在 UniApp 中使用 iframe 加載外部頁(yè)面時(shí),可能會(huì)遇到返回鍵行為不符合預(yù)期的問(wèn)題。這是因?yàn)?iframe 本身可以包含多個(gè)頁(yè)面的歷史記錄,而默認(rèn)情況下,瀏覽器的返回鍵會(huì)控制 iframe 內(nèi)部頁(yè)面的歷史記錄,而不是外部頁(yè)面的歷史記錄。
解決方案(禁用 iframe 的歷史記錄)
<template><view><iframe ref="myIframe" :src="iframeSrc" frameborder="0" @load="onIframeLoad"></iframe></view></template><script>export default {data() {return {iframeSrc: 'http://example.com',};},methods: {onIframeLoad(event) {if(this.$refs.myIframe.contentWindow){// 禁用 iframe 中的歷史記錄this.$refs.myIframe.contentWindow.history.pushState({}, '');this.$refs.myIframe.contentWindow.onpopstate = (event) => {event.preventDefault();// 跳轉(zhuǎn)到上一頁(yè)uni.switchTab({url: "/pages/index/index",});};}},}
};</script>