Google瀏覽器的一個新特點——關於擷取iframe的parent對象

來源:互聯網
上載者:User

        Google瀏覽器在調試JavaScript指令碼方面很方便,今天在使用Google瀏覽器調試代碼時無意間發現了一個奇特的問題:Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.,這主要是沒有擷取iframe所在所在頁面的parent的造成的,代碼如下:

        index.html:

       <html>

                 <head>

                            <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

                            <script type="text/javascript">

                                         function test() {

                                                alert('您成功調用了iframe標籤所在頁面的函數!');

                                         }

                           </script>

                 </head>

                 <body>

                          <iframe frameborder="1" src='child.html' style="width:50%; height:50%;" ></iframe>

                          <div style="font-size: 36px; margin-left: 40%; color: red;">iframe標籤所在頁面</div>

                 </body>

       </html>

       child.html:

       <html>

                   <head>

                           <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

                   </head>

                  <body>

                          <input type="button" onclick="javascript:window.parent.test();" value="調用iframe標籤所在頁面的函數"/>

                          <div style="font-size: 21px; margin-left: 40%; color: red;">iframe標籤所引用的頁面</div>

                  </body>

       </html>

       【0分下載代碼檔案】

       如果上面代碼在電腦上直接使用Google瀏覽器運行就會出現上面的錯誤“Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols,domains, and ports must match.”,而使用諸如IE瀏覽器直接存取則不會出現這種情況;如果將其使用伺服器(比如Tomcat)發布一下,再次使用Google瀏覽器通過http訪問index.html檔案則不會出現這種情況。

       總結:如果在本地用file協議使用Google瀏覽器開啟含有iframe標籤的頁面,則被iframe標籤引用的頁面無法訪問iframe標籤所在頁面的parent對象,除非使用伺服器(比如Tomcat)發布一下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.