自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JavaScript跨域總結(jié)與解決辦法

開發(fā) 前端
JavaScript出于安全方面的考慮,不允許跨域調(diào)用其他頁面的對象。但在安全限制的同時也給注入iframe或是ajax應(yīng)用上帶來了不少麻煩。這里把涉及到跨域的一些問題簡單地整理一下。

什么是跨域

JavaScript出于安全方面的考慮,不允許跨域調(diào)用其他頁面的對象。但在安全限制的同時也給注入iframe或是ajax應(yīng)用上帶來了不少麻煩。這里把涉及到跨域的一些問題簡單地整理一下:

首先什么是跨域,簡單地理解就是因為JavaScript同源策略的限制,a.com 域名下的js無法操作b.com或是c.a.com域名下的對象。更詳細的說明可以看下表:

JavaScript跨域

特別注意兩點:

第一,如果是協(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

  1. document.domain = 'a.com';  
  2. var ifr = document.createElement('iframe');  
  3. ifr.src = 'http://script.a.com/b.html';  
  4. ifr.style.display = 'none';  
  5. document.body.appendChild(ifr);  
  6. ifr.onload = function(){  
  7.     var doc = ifr.contentDocument || ifr.contentWindow.document;  
  8.     // 在這里操縱b.html  
  9.     alert(doc.getElementsByTagName("h1")[0].childNodes[0].nodeValue);  
  10. }; 

script.a.com上的b.html

  1. 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加載完畢的方法。

  1. js.onload = js.onreadystatechange = function() {  
  2.     if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') {  
  3.         // callback在此處執(zhí)行  
  4.         js.onload = js.onreadystatechange = null;  
  5.     }  
  6. }; 

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文件:

  1. function startRequest(){  
  2.     var ifr = document.createElement('iframe');  
  3.     ifr.style.display = 'none';  
  4.     ifr.src = 'http://www.cnblogs.com/lab/cscript/cs2.html#paramdo';  
  5.     document.body.appendChild(ifr);  
  6. }  
  7.  
  8. function checkHash() {  
  9.     try {  
  10.         var data = location.hash ? location.hash.substring(1) : '';  
  11.         if (console.log) {  
  12.             console.log('Now the data is '+data);  
  13.         }  
  14.     } catch(e) {};  
  15. }  
  16. setInterval(checkHash, 2000); 

cnblogs.com域名下的cs2.html:

  1. //模擬一個簡單的參數(shù)處理操作  
  2. switch(location.hash){  
  3.     case '#paramdo':  
  4.         callBack();  
  5.         break;  
  6.     case '#paramset':  
  7.         //do something……  
  8.         break;  
  9. }  
  10.  
  11. function callBack(){  
  12.     try {  
  13.         parent.location.hash = 'somedata';  
  14.     } catch (e) {  
  15.         // ie、chrome的安全機制無法修改parent.location.hash,  
  16.         // 所以要利用一個中間的cnblogs域下的代理iframe  
  17.         var ifrproxy = document.createElement('iframe');  
  18.         ifrproxy.style.display = 'none';  
  19.         ifrproxy.src = 'http://a.com/test/cscript/cs3.html#somedata';    // 注意該文件在"a.com"域下  
  20.         document.body.appendChild(ifrproxy);  
  21.     }  

a.com下的域名cs3.html

  1. //因為parent.parent和自身屬于同一個域,所以可以改變其location.hash的值  
  2. 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中的代碼:

  1. <iframe id="ifr" src="b.com/index.html"></iframe>  
  2. <script type="text/javascript">  
  3. window.onload = function() {  
  4.     var ifr = document.getElementById('ifr');  
  5.     var targetOrigin = 'http://b.com';  // 若寫成'http://b.com/c/proxy.html'效果一樣  
  6.                                         // 若寫成'http://c.com'就不會執(zhí)行postMessage了  
  7.     ifr.contentWindow.postMessage('I was there!', targetOrigin);  
  8. };  
  9. </script> 

b.com/index.html中的代碼:
 

  1. <script type="text/javascript">  
  2.     window.addEventListener('message'function(event){  
  3.         // 通過origin屬性判斷消息來源地址  
  4.         if (event.origin == 'http://a.com') {  
  5.             alert(event.data);    // 彈出"I was there!"  
  6.             alert(event.source);  // 對a.com、index.html中window對象的引用  
  7.                                   // 但由于同源策略,這里event.source不可以訪問window對象  
  8.         }  
  9.     }, false);  
  10. </script> 

原文鏈接:http://www.cnblogs.com/rainman/archive/2011/02/20/1959325.html

【編輯推薦】

  1. 使用Javascript開發(fā)移動應(yīng)用程序
  2. 用JavaScript 實現(xiàn)表格數(shù)據(jù)管理
  3. JavaScript版幾種常見排序算法分享
  4. JavaScript對象及繼承教程之內(nèi)置對象
  5. JavaScript內(nèi)存回收機制深入解讀
責(zé)任編輯:陳貽新 來源: Rain Man的博客
相關(guān)推薦

2009-02-18 09:30:10

AJAX跨域XML

2016-07-04 14:22:47

DevOps案例軟件

2011-08-05 08:53:26

路由器CSUDSU

2010-02-03 16:07:07

Ubuntu Auda

2021-04-27 15:20:41

人工智能機器學(xué)習(xí)技術(shù)

2011-04-08 09:16:12

JavaScript

2009-06-03 16:41:21

Eclipse亂碼Eclipse

2011-03-04 13:07:47

Filezilla

2011-06-17 11:10:51

Qt 中文 輸出

2010-01-30 09:05:11

Windows 7錯誤解決

2009-07-31 09:14:01

WinCE啟動失敗

2009-12-07 18:38:16

WCF異常

2011-04-27 16:04:12

投影機

2011-01-19 17:54:48

2017-08-20 12:49:59

瀏覽器跨域服務(wù)器

2019-04-10 10:32:16

CORSNginx反向代理

2025-03-05 11:00:00

JavaScript跨域前端

2010-01-05 18:03:57

2009-05-31 09:07:35

Oracle鎖定

2010-01-15 09:38:08

磁盤被寫保護解決辦法
點贊
收藏

51CTO技術(shù)棧公眾號