html页面中嵌套iframe网页时,如何真正返回到上一个页面

1.使用document.referrer代替history.back(-1) 返回

Document.referrer 返回的是一个 URI, 当前页面就是从这个 URI 所代表的页面 跳转或打开的.

如果用户直接打开了这个页面(不是通过页面跳转,而是通过地址栏或者书签等打开的),则该属性为空字符串。由于该属性只是返回一个字符串,所以不能够通过该属性引用页面的 DOM。
在中,Document.referrer 会初始化为父窗口Window.location的href。

goback(){

		window.location.href = window.document.referrer;
	}


                        
原文链接:https://blog.csdn.net/Octopus21/article/details/120298930

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于浏览器的安全限制,Vue页面无法直接向嵌套iframehtml页面传值。但是可以通过以下方式实现: 1. 使用postMessage 在Vue页面使用postMessage向iframe发送消息,iframehtml页面监听message事件,接收消息并进行处理。 Vue页面: ```javascript // 发送消息到iframe const iframeWindow = document.getElementById('iframe').contentWindow; iframeWindow.postMessage({data: 'hello'}, '*'); // 监听iframe返回的消息 window.addEventListener('message', (event) => { if (event.origin !== 'http://iframe-origin.com') return; // 验证消息来源 console.log(event.data); // 处理返回的消息 }); ``` iframe页面: ```javascript // 监听消息 window.addEventListener('message', (event) => { if (event.origin !== 'http://vue-origin.com') return; // 验证消息来源 console.log(event.data); // 处理接收到的消息 }); // 发送消息到Vue页面 const parentWindow = window.parent; parentWindow.postMessage({data: 'world'}, 'http://vue-origin.com'); ``` 2. 使用URL参数 在Vue页面嵌套iframe,可以在iframe的src属性添加参数,iframehtml页面可以通过URL参数获取传递的值。 Vue页面: ```html <iframe src="http://iframe-origin.com?data=hello"></iframe> ``` iframe页面: ```javascript // 获取URL参数 const urlParams = new URLSearchParams(window.location.search); const data = urlParams.get('data'); console.log(data); // 处理获取到的值 ``` 需要注意的是,使用URL参数传递数据,需要考虑参数的安全性,避免出现安全漏洞。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值