需求:
项目中需要通过iframe引入一个3D模型,点击模型时把对应位置的id传到当前页面。
问题:
当前页面的域名与通过iframe引入的页面域名不同,同源策略的限制所以不能使用localStorage,sessionStorage等。
解决方案: 使用postMessage
iframe模块中通过window.parent.postMessage发送信息,如下:
let id = 1001 parent.postMessage( id, '*')
需要接收信息的页面通过winodw.addEventListener接收message
window.addEventListener("message", this.receiveMsg, false)
网友评论