1.不跨域,攜帶sessionstorage打開
主頁面,存盤sessionstorage后,打開頁面
let data = {
text:'我是資料'
};
let isMobile = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)?true:false;
sessionStorage.setItem('information',JSON.stringify(data));
//ios不能打開新視窗,所以改為移動端在原本頁面打開,pc打開新視窗
window.open(window.location.protocol + "//" + window.location.host + reportUrl, isMobile?'_self':'_blank');
子頁面
var date = JSON.parse(sessionStorage.getItem('information'));
2.跨域,iframe通信
跨域的情況下,無法攜帶sessionstorage,通過iframe的postMessage通信機制傳遞資料;
不跨域也可以用,但更建議使用第一種,比較絲滑~
主頁面,寫入url,加載完成后,發送資料
<iframe id='iframe' class="iframe" v-if="src" ref="iframe" :src="src"></iframe>
let data = {
text:'我是資料'
};
this.src = url
this.$nextTick(()=>{
document.getElementById('iframe').onload=()=>{
document.getElementById('iframe').contentWindow.postMessage({
type:'preview',
data:data
},this.src)
document.getElementById('iframe').onload=null;
}
})
子頁面,執行監聽,created、mounted都可以
created() {
window.addEventListener('message',(event)=>{
console.log(event.data.type)
if(event.data&&event.data.type=='preview'){
console.log(event.data.data)
let data = JSON.stringify(event.data.data)
}
}, false);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/224296.html
標籤:其他
