亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

React常見跨窗口通信方式實例詳解

 更新時間:2022年10月17日 15:05:12   作者:jie19100  
這篇文章主要為大家介紹了React常見跨窗口通信方式實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

iframe

跨窗口通信就是在嵌套了iframe的時候,實現(xiàn)iframe與父窗口的通信。

什么是iframe

  • 它是一個html標簽,它可以將一個網(wǎng)站作為一個dom元素,嵌入到另一個網(wǎng)站中。
  • iframe具有自己的windowdocument對象。

使用場景

  • 比如公司開發(fā)了一個完整的網(wǎng)站,需要在另一個項目中去使用。比如直播功能,一些插件,這時候就可以使用iframe嵌入的方式。減少了重復開發(fā)的時間,需要修改界面的時候,也只需要修改一份代碼即可。
  • 微應用,微應用也有很多是使用iframe來實現(xiàn)。

同源策略

當兩個網(wǎng)站同時滿足:同協(xié)議+同域名+同端口的時候。

當iframe與父窗口同源時

  • 父窗口可以對iframe進行完全訪問,如windowdocument,location等對象的訪問。
  • 父窗口可以調(diào)用iframe的全局函數(shù)。
  • 父窗口可以修改iframe的元素內(nèi)容

效果圖

index1.html嵌套同源的index2.html

html1

<body>
  <h1>html-1</h1>
  <iframe src="http://127.0.0.1:3000/index2.html" frameborder="0"></iframe>
  <script>
    const iframe = document.querySelector("iframe");
    iframe.onload = function () {
      console.log(iframe)
      // 獲取iframe的window對象
      const iWindow = iframe.contentWindow;
      // 獲取iframe的document對象
      const iDocument = iframe.contentDocument;
      console.log(iWindow)
      console.log(iWindow.location)
      iWindow.say()
      iDocument.body.innerHTML = "<h1>this is html-1</h1>"
    }
  </script>
</body>

html2

<body>
  <h1>html-2</h1>
  <script>
    function say() {
      console.log("saying!!!")
    }
  </script>
</body>

效果圖

index1.html嵌套同源的index2.html

發(fā)現(xiàn)子iframewindow,document,location對象,以及子iframe的全局方法都可以訪問。

當iframe與父窗口不同源時

  • 父窗口無法訪問iframewindow的所有屬性與方法。
  • 父窗口無法訪問iframedocument
  • 無法調(diào)用iframe的全局方法。

效果圖

跨窗口通信

一:通過window.parent、frames、top

window.frames:獲取子iframe的列表,與document.querySelector("iframe")一樣

window.parent:獲取父window的引用

window.top:獲取最頂層窗口的window引用

上一節(jié)我們講到,當iframe同源時,不同窗口可以拿到對方的window對象,以及全局方法,那么我們可以利用全局方法來實現(xiàn)不同window窗口的通信。

html1

<body>
  <h1>html-1</h1>
  <div>
    <button onclick="send(Math.random(1))">發(fā)送數(shù)據(jù)給html2</button>
  </div>
  <iframe src="http://127.0.0.1:3000/index2.html" frameborder="0"></iframe>
  <script>
    const iframe = document.querySelector("iframe");
    let send;
    iframe.onload = function () {
      // 獲取iframe的window對象
      const iWindow = iframe.contentWindow;
      // 獲取iframe的document對象
      const iDocument = iframe.contentDocument;
      function receive(value) {
        console.log("這是html1,來了一條數(shù)據(jù):", value)
      }
      send = function (value) {
        iWindow.receive(value)
      }
    }
  </script>
</body>

html2

<body>
  <h1>html-2</h1>
  <div>
    <button onclick="send(Math.random(1))">發(fā)送數(shù)據(jù)給html1</button>
  </div>
  <script>
    function receive(value) {
      console.log("當前是html2,收到一條數(shù)據(jù):", value)
    }
    function send(value) {
      window.parent.receive(value)
    }
  </script>
</body>

效果圖

同理,window.top也可以這樣通信

二:window.postMessage

postMessage支持不同窗口之間的通信,即使是非同源的情況。

發(fā)送數(shù)據(jù)

當需要使用給其他窗口(window)發(fā)送數(shù)據(jù)時,需要調(diào)用對方windowpostMessage方法。

該方法接收兩個參數(shù)

  • 參數(shù)一:需要發(fā)送的數(shù)據(jù),數(shù)據(jù)最后為字符串形式,因為IE只支持字符串數(shù)據(jù)。
  • 參數(shù)二:接收方的地址(協(xié)議+域名+端口)

接收數(shù)據(jù)

監(jiān)聽message事件

該事件對象包含接收的數(shù)據(jù),以及發(fā)送方的地址等信息。

html1

<body>
  <h1>html-1</h1>
  <div>
    <button onclick="send(Math.random(1))">發(fā)送數(shù)據(jù)給html2</button>
  </div>
  <iframe src="http://127.0.0.1:3001/index2.html" frameborder="0"></iframe>
  <script>
    const iframe = document.querySelector("iframe");
    let send;
    iframe.onload = function () {
      // 獲取iframe的window對象
      const iWindow = iframe.contentWindow;
      send = function (value) {
        iWindow.postMessage("wuwuwuw", "http://127.0.0.1:3001")
      }
    }
    window.addEventListener("message", function (event) {
      if (event.origin != 'http://127.0.0.1:3001') {
        // 過濾指定地址的信息
        return;
      }
      if (window == event.source) {
        // 頁面初始化的時候會被瀏覽器觸發(fā)一次message,在這里根據(jù)發(fā)送方地址進行過濾
        return
      }
      console.log("html1收到的數(shù)據(jù) " + event.data);
    })
  </script>
</body>

html2

<body>
  <h1>html-2</h1>
  <div>
    <button onclick="send(Math.random(1))">發(fā)送數(shù)據(jù)給html1</button>
  </div>
  <script>
    function receive(value) {
      console.log("當前是html2,收到一條數(shù)據(jù):", value)
    }
    function send(value) {
      window.parent.postMessage(value, "http://127.0.0.1:3000")
    }
    window.addEventListener("message", function (event) {
      if (event.origin != 'http://127.0.0.1:3000') {
        // 過濾指定地址的信息
        return;
      }
      if (window == event.source) {
        // 頁面初始化的時候會被瀏覽器觸發(fā)一次message,在這里根據(jù)發(fā)送方地址進行過濾
        return;
      }
      console.log("html2收到的數(shù)據(jù) " + event.data);
    })
  </script>
</body>

效果圖

其他通信方法

  • 使用soket,需要后端支持
  • 使用本地存儲,監(jiān)聽本地存儲的數(shù)據(jù)變化

以上就是React常見跨窗口通信方式實例詳解的詳細內(nèi)容,更多關(guān)于React跨窗口通信方式的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • React項目中axios的封裝與API接口的管理詳解

    React項目中axios的封裝與API接口的管理詳解

    Axios是一個npm軟件包,允許應用程序?qū)TTP請求發(fā)送到Web API,下面這篇文章主要給大家介紹了關(guān)于React項目中axios的封裝與API接口的管理的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • react實現(xiàn)antd線上主題動態(tài)切換功能

    react實現(xiàn)antd線上主題動態(tài)切換功能

    這篇文章主要介紹了react實現(xiàn)antd線上主題動態(tài)切換功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • 在react中使用tailwind的問題

    在react中使用tailwind的問題

    這篇文章主要介紹了在react中使用tailwind的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 使用React?Router?v6?添加身份驗證的方法

    使用React?Router?v6?添加身份驗證的方法

    這篇文章主要介紹了使用React?Router?v6?進行身份驗證完全指南,本文將演示如何使用React?Router?v6創(chuàng)建受保護的路由以及如何添加身份驗證,需要的朋友可以參考下
    2022-05-05
  • React 原理詳解

    React 原理詳解

    這篇文章主要介紹了深入理解react的原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-10-10
  • react-router-domV6版本改版踩坑記錄

    react-router-domV6版本改版踩坑記錄

    這篇文章主要介紹了react-router-domV6版本改版踩坑記錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • React如何立即更新DOM

    React如何立即更新DOM

    這篇文章主要介紹了React如何立即更新DOM問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 一篇文章教你用React實現(xiàn)菜譜系統(tǒng)

    一篇文章教你用React實現(xiàn)菜譜系統(tǒng)

    本篇文章主要介紹了React實現(xiàn)菜譜軟件的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-09-09
  • React?Native采用Hermes熱更新打包方案詳解

    React?Native采用Hermes熱更新打包方案詳解

    這篇文章主要介紹了React?Native采用Hermes熱更新打包實戰(zhàn),在傳統(tǒng)的熱更新方案中,我們實現(xiàn)熱更新需要借助code-push開源方案,包括熱更新包的發(fā)布兩種方式詳解,感興趣的朋友一起看看吧
    2022-05-05
  • React封裝全屏彈框的方法

    React封裝全屏彈框的方法

    這篇文章主要為大家詳細介紹了React封裝全屏彈框的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08

最新評論