javascript跨域的方法匯總
此文章學(xué)習(xí)借鑒了一些其他前端同學(xué)的文章,自己做了個(gè)實(shí)踐總結(jié)
以下的例子包含的文件均為為 http://www.a.com/a.html 、http://www.a.com/c.html 與 http://www.b.com/b.html,要做的都是從a.html獲取b.html里的數(shù)據(jù)
1.JSONP
jsonp是利用script標(biāo)簽沒(méi)有跨域限制的特性,通過(guò)在src的url的參數(shù)上附加回調(diào)函數(shù)名字,然后服務(wù)器接收回調(diào)函數(shù)名字并返回一個(gè)包含數(shù)據(jù)的回調(diào)函數(shù)
function doSomething(data) {
// 對(duì)data處理
}
var script = document.createElement("script");
script.src = "http://www.b.com/b.html?callback=doSomething";
document.body.appendChild(script);
// 1.生成一個(gè)script標(biāo)簽,將其append在body上,向服務(wù)器發(fā)出請(qǐng)求
// 2.服務(wù)器根據(jù) callback 這個(gè)參數(shù)生成一個(gè)包含數(shù)據(jù)的函數(shù) doSomething({"a", "1"})
// 3.頁(yè)面事先已聲明doSomething函數(shù),此時(shí)執(zhí)行 doSomething(data) 這個(gè)函數(shù),獲得數(shù)據(jù)
2.HTML5的postMessage
假設(shè)在a.html里嵌套個(gè)<iframe src="
a.html b.html 這樣打開(kāi)a頁(yè)面就先彈出 a data,再?gòu)棾?b data 3.window.name + iframe window.name的原理是利用同一個(gè)窗口在不同的頁(yè)面共用一個(gè)window.name,這個(gè)需要在a.com下建立一個(gè)代理文件c.html,使同源后a.html能獲取c.html的window.name a.html b.html 4.window.location.hash + iframe b.html將數(shù)據(jù)以hash值的方式附加到c.html的url上,在c.html頁(yè)面通過(guò)location.hash獲取數(shù)據(jù)后傳到a.html(這個(gè)例子是傳到a.html的hash上,當(dāng)然也可以傳到其他地方) a.html b.html c.html 5.CORS CORS是XMLHttpRequest Level 2 里規(guī)定的一種跨域方式。在支持這個(gè)方式的瀏覽器里,javascript的寫(xiě)法和不跨域的ajax寫(xiě)法一模一樣,只要服務(wù)器需要設(shè)置Access-Control-Allow-Origin: * 6.document.domain 這種方式適用于主域相同,子域不同,比如http://www.a.com和http://b.a.com a.html b.html 注意:document.domain需要設(shè)置成自身或更高一級(jí)的父域,且主域必須相同。
window.onload = function() {
window.addEventListener("message", function(e) {
alert(e.data);
});
window.frames[0].postMessage("b data", "http://www.b.com/b.html");
}
window.onload = function() {
window.addEventListener("message", function(e) {
alert(e.data);
});
window.parent.postMessage("a data", "http://www.a.com/a.html");
}
var iframe = document.createElement("iframe");
iframe.src = "http://www.b.com/b.html";
document.body.appendChild(iframe); // 現(xiàn)在a.html里建一個(gè)引用b.html的iframe,獲得b的數(shù)據(jù)
var flag = true;
iframe.onload = function() {
if (flag) {
iframe.src = "c.html";
// 判斷是第一次載入的話,設(shè)置代理c.html使和a.html在同目錄同源,這樣才能在下面的else取到data
flag = false;
} else { // 第二次載入由于a和c同源,a可以直接獲取c的window.name
alert(iframe.contentWindow.name);
iframe.contentWindow.close();
document.body.removeChild(iframe);
iframe.src = '';
iframe = null;
}
}
window.name = "這是 b 頁(yè)面的數(shù)據(jù)";
var iframe = document.createElement("iframe");
iframe.src = "http://www.b.com/b.html";
document.body.appendChild(iframe); // 在a頁(yè)面引用b
function check() { // 設(shè)置個(gè)定時(shí)器不斷監(jiān)控hash的變化,hash一變說(shuō)明數(shù)據(jù)傳過(guò)來(lái)了
var hashs = window.location.hash;
if (hashs) {
clearInterval(time);
alert(hashs.substring(1));
}
}
var time = setInterval(check, 30);
window.onload = function() {
var data = "this is b's data";
var iframe = document.createElement("iframe");
iframe.src = "http://www.a.com/c.html#" + data;
document.body.appendChild(iframe); // 將數(shù)據(jù)附加在c.html的hash上
}
// 獲取自身的hash再傳到a.html的hash里,數(shù)據(jù)傳輸完畢
parent.parent.location.hash = self.location.hash.substring(1);
假如這兩個(gè)域名下各有a.html 和b.html,
document.domain = "a.com";
var iframe = document.createElement("iframe");
iframe.src = "http://b.a.com/b.html";
document.body.appendChild(iframe);
iframe.onload = function() {
console.log(iframe.contentWindow....); // 在這里操作b.html里的元素?cái)?shù)據(jù)
}
document.domain = "a.com";
相關(guān)文章
10個(gè)基于瀏覽器的JavaScript調(diào)試工具分享
調(diào)試Javascript可能是web開(kāi)發(fā)中最讓人郁悶的事情,這里是10款我們精選的基于瀏覽器的JS在線調(diào)試工具,感興趣的朋友可以參考下,或許對(duì)你有所幫助2013-02-02
JS實(shí)現(xiàn)手寫(xiě)parseInt的方法示例
這篇文章主要給大家介紹了JS實(shí)現(xiàn)手寫(xiě)parseInt的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
開(kāi)發(fā)中常用的25個(gè)JavaScript單行代碼(小結(jié))
這篇文章主要介紹了開(kāi)發(fā)中常用的25個(gè)JavaScript單行代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
微信JS-SDK坐標(biāo)位置如何轉(zhuǎn)換為百度地圖坐標(biāo)
這篇文章主要介紹了微信JS-SDK坐標(biāo)位置如何轉(zhuǎn)換為百度地圖坐標(biāo) 的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07
javascript開(kāi)發(fā)隨筆二 動(dòng)態(tài)加載js和文件
js無(wú)非就是script標(biāo)簽引入頁(yè)面,但當(dāng)項(xiàng)目越來(lái)越大的時(shí)候,單頁(yè)面引入N個(gè)js顯然不行,合并為單個(gè)文件減少了請(qǐng)求數(shù),但請(qǐng)求的文件體積卻很大2011-11-11
javaScript 頁(yè)面自動(dòng)加載事件詳解
本篇文章主要是對(duì)javaScript頁(yè)面自動(dòng)加載事件進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02

