“死板“的Chrome,這樣也算跨域

來源:互聯網
上載者:User

今天在罈子看到一文章,是說在

Chrome下,不能擷取iframe的contentWindow。

 

如果沒有跨域的話,這是不可能的,肯定能擷取的到才對,所以我寫了下面兩個測試頁面。

 

page A :

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br /><title>無標題文檔</title><br /><mce:style type="text/css"><!--<br />*{padding:0;margin:0;}</p><p>--></mce:style><style type="text/css" mce_bogus="1">*{padding:0;margin:0;}<br /></style><br /></head><br /><body><br /><mce:script type="text/javascript"><!--<br /> function show(){<br /> var _iframe = document.getElementById('aa');<br />_iframe.contentWindow.b();<br /> }<br />function a() {<br />alert('page a');<br />}<br />// --></mce:script><br /><iframe id="aa" name="aa" src="4.html" mce_src="4.html" frameborder="0" scrolling="no" width="100%" height="200" ></iframe><br /><input type="button" value="click" onclick="show()"/><br /></body><br /></html>

 

 

page B :

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br /><title>無標題文檔</title><br /><mce:style type="text/css"><!--<br />*{padding:0;margin:0;}<br />--></mce:style><style type="text/css" mce_bogus="1">*{padding:0;margin:0;}</style><br /></head><br /><body><br /><mce:script type="text/javascript"><!--<br />(function() {<br />parent.window.a();<br />})();<br />function b() {<br />alert('page b');<br />}<br />// --></mce:script><br /></body><br /></html><br />

 

結果測試了幾個常用的瀏覽器:FF3.6,IE6,Opera10,Safair, Chrome

 

除了最後一個Chrome,其它的都能正常運行,得到正確的結果。

 

無語中。。。

 

於是我開啟

Chrome的JS控制器,發現顯示了一條JS錯誤:

 

Unsafe JavaScript attempt to access frame with URL file:///D:/test/4.html

from frame with URL file:///D:/test/3.html

. Domains, protocols and ports must match.


意思是說:

在頁面3.html和頁面4.html之間的javascript是不安全的。域,

協議和連接埠

必須匹配

才可以。

 

你說暈不暈...

 

其它的瀏覽器都認可直接在瀏覽器中開啟頁面3.html時(url=="file:///D:/test/3.html"),和3.html在同一檔案夾內的4.html是同域,但


Chrome卻不認。

 



所以我只好開了丟在一邊很久的IIS。

在瀏覽器中輸入:http://localhost/test/3.html

 

測試通過。。。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.