iframe標籤使用傳遞參數

父級給iframe內部傳遞參數

方法1:很簡單,直接在iframe的src數屬性中拼接上參數

// 發送方:
<div>
    <iframe :src="url" id="childFrame" importance="high" name="demo" ></iframe>
</div>

url = `http://localhost:8080/couponManager?a={a}` //  

接收方:直接使用window.location.search接收,然後對接收到的進行處理

未完待續…

iframe內部給父級傳遞參數

方法1:使用postMessage方法跨域傳遞

// 發送方:
top.postMessage("hello", "http://localhost:8081"); // 要寫top.postMessage或者是parent.message

// 接收方:
window.addEventListener( "message",
(e)=>{console.log(e.data)}	
false);

未完待續…

遇到的問題

1、問題:Failed to execute ‘postMessage’ on ‘DOMWindow’
如果遇到上面的問題可以看下這位大佬的解釋:Failed to execute ‘postMessage’ on ‘DOMWindow’

上面的方法都是我自己親自試過的,完全可用,如果小夥伴還有比較好的方法,可以留言給我,萬分感謝~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章