需求:
项目中需要通过iframe引入一个3D模型,点击模型时把对应位置的id传到当前页面。
问题:
当前页面的域名与通过iframe引入的页面域名不同,同源策略的限制所以不能使用localStorage,sessionStorage等。
解决方案: 使用postMessage
iframe模块中通过window.parent.postMessage发送信息,如下:
let id = 1001parent.postMessage( id, '*')
需要接收信息的页面通过winodw.addEventListener接收message
window.addEventListener("message", this.receiveMsg, false)
文章转载于:https://www.jianshu.com/p/78048f0fbbcd
原著是一个有趣的人,若有侵权,请通知删除
还没有人抢沙发呢~