JavaScript跨域總結(jié)與解決辦法
什么是跨域
JavaScript出于安全方面的考慮,不允許跨域調(diào)用其他頁面的對象。但在安全限制的同時也給注入iframe或是ajax應(yīng)用上帶來了不少麻煩。這里把涉及到跨域的一些問題簡單地整理一下:
首先什么是跨域,簡單地理解就是因為JavaScript同源策略的限制,a.com 域名下的js無法操作b.com或是c.a.com域名下的對象。更詳細的說明可以看下表:
特別注意兩點:
第一,如果是協(xié)議和端口造成的跨域問題“前臺”是無能為力的,
第二:在跨域問題上,域僅僅是通過“URL的首部”來識別而不會去嘗試判斷相同的ip地址對應(yīng)著兩個域或兩個域是否在同一個ip上。
“URL的首部”指window.location.protocol +window.location.host,也可以理解為“Domains, protocols and ports must match”。
接下來簡單地總結(jié)一下在“前臺”一般處理跨域的辦法,后臺proxy這種方案牽涉到后臺配置,這里就不闡述了,有興趣的可以看看yahoo的這篇文章:《JavaScript: Use a Web Proxy for Cross-Domain XMLHttpRequest Calls》
1、document.domain+iframe的設(shè)置
對于主域相同而子域不同的例子,可以通過設(shè)置document.domain的辦法來解決。具體的做法是可以在http://www.a.com/a.html和http://script.a.com/b.html兩個文件中分別加上document.domain = ‘a.com’;然后通過a.html文件中創(chuàng)建一個iframe,去控制iframe的contentDocument,這樣兩個js文件之間就可以“交互”了。當(dāng)然這種辦法只能解決主域相同而二級域名不同的情況,如果你異想天開的把script.a.com的domian設(shè)為alibaba.com那顯然是會報錯地!代碼如下:
www.a.com上的a.html
- document.domain = 'a.com';
- var ifr = document.createElement('iframe');
- ifr.src = 'http://script.a.com/b.html';
- ifr.style.display = 'none';
- document.body.appendChild(ifr);
- ifr.onload = function(){
- var doc = ifr.contentDocument || ifr.contentWindow.document;
- // 在這里操縱b.html
- alert(doc.getElementsByTagName("h1")[0].childNodes[0].nodeValue);
- };
script.a.com上的b.html
- document.domain = 'a.com';
這種方式適用于{www.kuqin.com, kuqin.com, script.kuqin.com, css.kuqin.com}中的任何頁面相互通信。
備注:某一頁面的domain默認等于window.location.hostname。主域名是不帶www的域名,例如a.com,主域名前面帶前綴的通常都為二級域名或多級域名,例如www.a.com其實是二級域名。 domain只能設(shè)置為主域名,不可以在b.a.com中將domain設(shè)置為c.a.com。
問題:
1、安全性,當(dāng)一個站點(b.a.com)被攻擊后,另一個站點(c.a.com)會引起安全漏洞。
2、如果一個頁面中引入多個iframe,要想能夠操作所有iframe,必須都得設(shè)置相同domain。
2、動態(tài)創(chuàng)建script
雖然瀏覽器默認禁止了跨域訪問,但并不禁止在頁面中引用其他域的JS文件,并可以自由執(zhí)行引入的JS文件中的function(包括操作cookie、Dom等等)。根據(jù)這一點,可以方便地通過創(chuàng)建script節(jié)點的方法來實現(xiàn)完全跨域的通信。具體的做法可以參考YUI的Get Utility
這里判斷script節(jié)點加載完畢還是蠻有意思的:ie只能通過script的readystatechange屬性,其它瀏覽器是script的load事件。以下是部分判斷script加載完畢的方法。
- js.onload = js.onreadystatechange = function() {
- if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') {
- // callback在此處執(zhí)行
- js.onload = js.onreadystatechange = null;
- }
- };
3、利用iframe和location.hash
這個辦法比較繞,但是可以解決完全跨域情況下的腳步置換問題。原理是利用location.hash來進行傳值。在url: http://a.com#helloword中的‘#helloworld’就是location.hash,改變hash并不會導(dǎo)致頁面刷新,所以可以利用hash值來進行數(shù)據(jù)傳遞,當(dāng)然數(shù)據(jù)容量是有限的。假設(shè)域名a.com下的文件cs1.html要和cnblogs.com域名下的cs2.html傳遞信息,cs1.html首先創(chuàng)建自動創(chuàng)建一個隱藏的iframe,iframe的src指向cnblogs.com域名下的cs2.html頁面,這時的hash值可以做參數(shù)傳遞用。cs2.html響應(yīng)請求后再將通過修改cs1.html的hash值來傳遞數(shù)據(jù)(由于兩個頁面不在同一個域下IE、Chrome不允許修改parent.location.hash的值,所以要借助于a.com域名下的一個代理iframe;Firefox可以修改)。同時在cs1.html上加一個定時器,隔一段時間來判斷l(xiāng)ocation.hash的值有沒有變化,一點有變化則獲取獲取hash值。代碼如下:
先是a.com下的文件cs1.html文件:
- function startRequest(){
- var ifr = document.createElement('iframe');
- ifr.style.display = 'none';
- ifr.src = 'http://www.cnblogs.com/lab/cscript/cs2.html#paramdo';
- document.body.appendChild(ifr);
- }
- function checkHash() {
- try {
- var data = location.hash ? location.hash.substring(1) : '';
- if (console.log) {
- console.log('Now the data is '+data);
- }
- } catch(e) {};
- }
- setInterval(checkHash, 2000);
cnblogs.com域名下的cs2.html:
- //模擬一個簡單的參數(shù)處理操作
- switch(location.hash){
- case '#paramdo':
- callBack();
- break;
- case '#paramset':
- //do something……
- break;
- }
- function callBack(){
- try {
- parent.location.hash = 'somedata';
- } catch (e) {
- // ie、chrome的安全機制無法修改parent.location.hash,
- // 所以要利用一個中間的cnblogs域下的代理iframe
- var ifrproxy = document.createElement('iframe');
- ifrproxy.style.display = 'none';
- ifrproxy.src = 'http://a.com/test/cscript/cs3.html#somedata'; // 注意該文件在"a.com"域下
- document.body.appendChild(ifrproxy);
- }
- }
a.com下的域名cs3.html
- //因為parent.parent和自身屬于同一個域,所以可以改變其location.hash的值
- parent.parent.location.hash = self.location.hash.substring(1);
當(dāng)然這樣做也存在很多缺點,諸如數(shù)據(jù)直接暴露在了url中,數(shù)據(jù)容量和類型都有限等……
4、window.name實現(xiàn)的跨域數(shù)據(jù)傳輸
文章較長列在此處不便于閱讀,詳細請看 window.name實現(xiàn)的跨域數(shù)據(jù)傳輸。
5、使用HTML5 postMessage
HTML5中最酷的新功能之一就是 跨文檔消息傳輸Cross Document Messaging。下一代瀏覽器都將支持這個功能:Chrome 2.0+、Internet Explorer 8.0+, Firefox 3.0+, Opera 9.6+, 和 Safari 4.0+ 。 Facebook已經(jīng)使用了這個功能,用postMessage支持基于web的實時消息傳遞。
otherWindow.postMessage(message, targetOrigin);
otherWindow: 對接收信息頁面的window的引用??梢允琼撁嬷衖frame的contentWindow屬性;window.open的返回值;通過name或下標(biāo)從window.frames取到的值。
message: 所要發(fā)送的數(shù)據(jù),string類型。
targetOrigin: 用于限制otherWindow,“*”表示不作限制
a.com/index.html中的代碼:
- <iframe id="ifr" src="b.com/index.html"></iframe>
- <script type="text/javascript">
- window.onload = function() {
- var ifr = document.getElementById('ifr');
- var targetOrigin = 'http://b.com'; // 若寫成'http://b.com/c/proxy.html'效果一樣
- // 若寫成'http://c.com'就不會執(zhí)行postMessage了
- ifr.contentWindow.postMessage('I was there!', targetOrigin);
- };
- </script>
b.com/index.html中的代碼:
- <script type="text/javascript">
- window.addEventListener('message', function(event){
- // 通過origin屬性判斷消息來源地址
- if (event.origin == 'http://a.com') {
- alert(event.data); // 彈出"I was there!"
- alert(event.source); // 對a.com、index.html中window對象的引用
- // 但由于同源策略,這里event.source不可以訪問window對象
- }
- }, false);
- </script>
原文鏈接:http://www.cnblogs.com/rainman/archive/2011/02/20/1959325.html
【編輯推薦】