對于Web應(yīng)用來說,瀏覽器是最重要的客戶端。
目前瀏覽器五花八門多得不得了,除了Chrome、IE、Firefox、Safari、Opera這些國外的瀏覽器外,百度、騰訊、360、淘寶、搜狗、傲游之類的,反正能做的都做了。
瀏覽器雖然這么多,但瀏覽器內(nèi)核主要就以下4種:
國內(nèi)的瀏覽器基本都是雙核瀏覽器,使用基于WebKit的內(nèi)核高速瀏覽常用網(wǎng)站,使用Trident內(nèi)核兼容網(wǎng)銀等網(wǎng)站。
同源策略是瀏覽器最基本的安全策略,它認為任何站點的內(nèi)容都是不安全的,所以當腳本運行時,只被允許訪問來自同一站點的資源。
同源是指域名、協(xié)議、端口都相同。
如果沒有同源策略,就會發(fā)生下面這樣的問題:
惡意網(wǎng)站用一個iframe把真實的銀行登錄頁放到他的頁面上,當用戶使用用戶名密碼登錄時,父頁面的javascript就可以讀取到銀行登錄頁表單中的內(nèi)容。
甚至瀏覽器的1個Tab頁打開了惡意網(wǎng)站,另一個Tab頁打開了銀行網(wǎng)站,惡意網(wǎng)站中的javascript可以讀取到銀行網(wǎng)站的內(nèi)容。這樣銀行卡和密碼就能被輕易拿走。
由于同源策略的原因,瀏覽器對跨域訪問做了很多限制,但有時我們的確需要做跨域訪問,那要怎么辦?主要有以下幾種情況:
同域名下,父頁面可以通過document.getElementById(‘_iframe’).contentWindow.document訪問子頁面的內(nèi)容,但不同域名下會出現(xiàn)類似下面的錯誤:
Uncaught SecurityError: Blocked a frame with origin “http://a.com” from accessing a frame with origin “http://b.com”. Protocols, domains, and ports must match.
1). 當主域名相同,子域名不同時,比較容易解決,只需設(shè)置相同的document.domain即可。
如http://a.a.com/a.html使用iframe載入http://b.a.com/b.html,且在a.html中有Javascript要修改b.html中元素的內(nèi)容時,可以像下面的代碼那樣操作。
a.html
<html>
<head>
<script>
document.domain = 'a.com';
function changeIframeContent() {
var _iframe = document.getElementById('_iframe');
var _p = _iframe.contentWindow.document.getElementById('_p');
_p.innerHTML = 'Content from a.html';
}
</script>
</head>
<body>
<iframe id="_iframe" src="http://b.a.com/demo/iframe/subdomain/b.html"></iframe>
<br>
<input type="button" value="Change iframe content" onclick="changeIframeContent();"/>
</body>
</html>
b.html
<html>
<head>
<script>
document.domain = 'a.com';
</script>
</head>
<body>
<p id="_p">b.html</p>
</body>
</html>
2). 當主域名不同時,就非常麻煩了。大致的方法像下面描述的那樣:
Ajax主要通過XMLHttpRequest對象實現(xiàn),但是如果通過XMLHttpRequest訪問不同域名下的數(shù)據(jù),瀏覽器會出現(xiàn)類似下面的錯誤:
XMLHttpRequest cannot load http://b.com/demo/iframe/ajax/b.html. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://a.com’ is therefore not allowed access.
1). 使用<script>代替XMLHttpRequest,也就是JSONP的方法。利用<script>標簽的src下加載的js不受同源策略限制,并且加載后的js運行在當前頁面的域下,所以可自由操作當前頁面的內(nèi)容。
下面的代碼演示了在a.com下的a.html通過b.com下的b.js中的內(nèi)容來更新自身的p標簽。
a.html
<html>
<head>
<script>
function update_p (content) {
document.getElementById("_p").innerHTML = content;
}
function getFromB() {
var _script = document.createElement("script");
_script.type = "text/javascript";
_script.src = "http://b.com/demo/ajax/b.js";
document.getElementsByTagName("head")[0].appendChild(_script);
}
</script>
</head>
<body>
<p id="_p">a.html</p>
<input type="button" value="Get from b.com" onclick="getFromB()"/>
</body>
</html>
b.js
update_p("content from b.js");
在實際使用中,通常a.html會將update_p以callback參數(shù)名傳遞給b.com的服務(wù)器,服務(wù)器動態(tài)生成數(shù)據(jù)后,再用callback參數(shù)值包起來作為響應(yīng)回傳給a.html。
2). 在b.com的服務(wù)器返回信息中增加以下頭信息:
此時瀏覽器便允許a.com讀取使用GET請求b.com的內(nèi)容。
對于flash來說,會要求在網(wǎng)站根目錄下放一個名為crossdomain.xml的文件,以指明允許訪問的域名來源。文件中的內(nèi)容類似下面的樣子:
<cross-domain-policy>
<allow-access-from domain="*.a.com" />
</cross-domain-policy>
HTML5增加了跨文檔消息傳輸,下面的例子實現(xiàn)了使用postMessage在不同域間傳遞消息:
a.html
<html>
<head>
<script>
function update_b () {
var _iframe = document.getElementById("_iframe");
_iframe.contentWindow.postMessage("content from a.html", "http://b.com");
}
</script>
<head>
<body>
<iframe id="_iframe" src="http://b.com/demo/html5/b.html"></iframe>
<br>
<input type="button" value="Update b.html" onclick="update_b()"></input>
</body>
</html>
b.html
<html>
<head>
<script>
window.addEventListener("message", function (event) {
document.getElementById("_p").innerHTML = event.data;
}, false);
</script>
</head>
<body>
<p id="_p">b.html</p>
</body>
</html>
在postMessage中要指定接收方的域名,如果發(fā)現(xiàn)目標頁面的域名不正確,將拋出類似下面這樣的錯誤:
Failed to execute ‘postMessage’ on ‘DOMWindow’: The target origin provided (‘http://c.com’) does not match the recipient window’s origin (‘http://b.com’).
瀏覽器對跨域訪問的限制是出于安全考慮的,所以在使用一些方法實現(xiàn)跨域訪問時要特別小心。