js跨域問題之跨域iframe自適應(yīng)大小實現(xiàn)代碼
更新時間:2010年07月17日 18:29:52 作者:
前幾天做公司和開心網(wǎng)合作項目的時候 碰到iframe 跨域自適應(yīng)的問題剛開始很迷惑 開心網(wǎng)那邊技術(shù)工程師給我發(fā)了一段這樣子的代碼。
復(fù)制代碼 代碼如下:
<body onload="javascript: setHeight();">
<script>
function setHeight(){
var dHeight = document.documentElement.scrollHeight;
var t = document.createElement("div");
t.innerHTML = '<iframe id="kxiframeagent" src="http://rest.kaixin001.com/api/agent.html#'+dHeight+'"scrolling="yes" height="0px" width="0px"></iframe>';
document.documentElement.appendChild(t.firstChild);
}
</script>
我想著跨域如何解決啊!后來去網(wǎng)上搜索了一下子 恍然大悟
具體如下 我是復(fù)制過來了 大家重點理解一下子它的實現(xiàn)思路 :
問題:
A域名下的頁面a.htm中通過iframe嵌入B域名下的頁面b.html,由于b.html的大小等是不可預(yù)知而且會變化的,所以需要 a.htm中的iframe自適應(yīng)大小.
問題本質(zhì) :
js的跨域問題,因為要控制a.htm中iframe的大小就必須首先讀取得到b.html的大小,A、B不屬于同一個域,js的訪問受限,讀取不 到b.html的大小.
解決方案:
首先前提是A,B是合作關(guān)系,b.html中能引入A提供的js
首先a.html中通過iframe引入了b.html
復(fù)制代碼 代碼如下:
<iframe id="aIframe" height="0" width="0" src="http://www.b.com/html/b.html" frameborder="no" border="0px" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes" ></iframe>
<iframe id="aIframe" height="0" width="0" src="http://www.b.com/html/b.html" frameborder="no" border="0px" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes" ></iframe>
B在b.html中引入了A提供的js文件
Html代碼
復(fù)制代碼 代碼如下:
<script language="javascript" type="text/javascript" src="http://www.a.com/js/a.js"></script>
該js首先讀取b.html的寬和高,然后創(chuàng)建一個iframe,src為和A同一個域的中間代理頁面a_proxy.html,吧讀取到的寬和 高設(shè)置到src的hash里面
Html代碼
復(fù)制代碼 代碼如下:
<iframe id="iframeProxy" height="0" width="0" src="http://www.a.com/html/a_proxy.html#width|height" style="display:none" ></iframe>
a_proxy.html是A域下提供好的中間代理頁面,它負(fù)責(zé)讀取location.hash里面的width和height的值,然后設(shè)置與 它同域下的a.html中的iframe的寬和高.
Js代碼
復(fù)制代碼 代碼如下:
var pParentFrame = parent.parent.document.getElementById("aIframe");
var locationUrlHash = parent.parent.frames["aIframe"].frames["iframeProxy"].location.hash;
pParentFrame.style.width = locationUrlHash.split("#")[1].split("|")[0]+"px";
pParentFrame.style.height = locationUrlHash.split("#")[1].split("|")[1]+"px";
var pParentFrame = parent.parent.document.getElementById("aIframe");
var locationUrlHash = parent.parent.frames["aIframe"].frames["iframeProxy"].location.hash;
pParentFrame.style.width = locationUrlHash.split("#")[1].split("|")[0]+"px";
pParentFrame.style.height = locationUrlHash.split("#")[1].split("|")[1]+"px";
這樣的話a.html中的iframe就自適應(yīng)為b.html的寬和高了.
其他一些類似js跨域操作問題也可以按這個思路去解決
您可能感興趣的文章:
相關(guān)文章
面向JavaScript入門初學(xué)者的二叉搜索樹算法教程
二叉搜索樹則是二叉樹的一種,但它只允許你在左側(cè)節(jié)點儲存比父節(jié)點小的值,右側(cè)只允許儲存比父節(jié)點大的值,這篇文章主要給大家介紹了關(guān)于JavaScript二叉搜索樹算法的相關(guān)資料,需要的朋友可以參考下2021-09-09JavaScript檢查數(shù)字是否為整數(shù)或浮點數(shù)的方法
這篇文章主要介紹了JavaScript檢查數(shù)字是否為整數(shù)或浮點數(shù)的方法,涉及javascript類型判斷的相關(guān)技巧,需要的朋友可以參考下2015-06-06JS實現(xiàn)兩個大數(shù)(整數(shù))相乘
大數(shù),即超出語言所能表示的數(shù)字最大范圍的數(shù)字,那么如何實現(xiàn)兩個大數(shù)相乘呢?下面有個不錯的方法,大家可以參考下2014-04-04JavaScript之json_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了JavaScript之json,JSON它是一種數(shù)據(jù)交換格式。有興趣的可以了解一下2017-06-06Express代理轉(zhuǎn)發(fā)服務(wù)器實現(xiàn)
這篇文章主要為大家介紹了Express代理轉(zhuǎn)發(fā)服務(wù)器實現(xiàn)技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05D3.js 實現(xiàn)帶伸縮時間軸拓?fù)鋱D的示例代碼
這篇文章主要介紹了D3.js 實現(xiàn)帶伸縮時間軸拓?fù)鋱D的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01