大家都知道JS有同源策略,主域名稱不同的iframe不允許JS直接通訊。
比如我有一個網站,要在其中嵌入其他網站的頁面,可以用 iframe 引用第三方網址。但問題也來了:iframe 的高度是固定的,無法與第三方網站好好融合;又例如第三方網站用了瀑布流插件、需要滾動加載自動計算高度,這時該怎麼辦?
假如網站 A.com 裡放入一個 iframe 引用了 B.com,這種情況下 B.com 裡的JS是無法存取 A.com 的。JS跨域必須同源,也就是同一個主域名稱,那該怎麼辦呢?
我們可以在 B.com 中再引入一個 iframe(暫且叫 C),這個 iframe 加載 A.com 中的一個網頁。這樣 B.com 中的 iframe 網頁就能和 A.com 通訊了。接下來只要讓 B 和 C 通訊、C 和 A 通訊,就完成了 B→A 的通訊鏈路。如此一來,當 B 高度變化時通知 C,讓 C 通知 A 調整 iframe 高度即可。而 B 和 C 的通訊,其實透過改變 src 的網址參數就能實現。
廢話不多說,直接上程式碼。
<html>
<script src="{$smarty.const.STATIC_URL}/js/jquery-1.7.1.min.js"></script>
<script>
var test = function() {
$('#h1').html('test');
}
</script>
<body>
<h1 id="h1">nba.alltosun.net</h1>
<iframe id="ifm" width="760" height="100" src="https://***.sinaapp.com/"></iframe>
</body>
</html>
<html>
<head></head>
<body>
<h1>**.appsina.com</h1>
<button id="button">設置高度</button>
<div id="div" style="height:200px;display:none;"></div>
<script src="https://nba.alltosun.net/js/jquery-1.7.1.min.js"></script>
<script>
$(function(){
window.ifrH = function(e){
var searchUrl = window.location.search;
var b = null;
var getParam = function(url, param) {
var q = url.match(new RegExp( param + "=[^&]*")), n = "";
if (q && q.length) {
n = q[0].replace(param + "=", "");
}
return n;
}
var f = getParam(searchUrl,"url"), h = getParam(searchUrl, "ifmID"), k = getParam(searchUrl, "cbn"), g = getParam(searchUrl, "mh");
var iframeId = 'testiframe';
var iframe = document.getElementById(iframeId);
var divId = 'alltosun';
if (!iframe){
var iframe = document.createElement('iframe');
iframe.id = iframeId;
iframe.style.display = "none";
iframe.width = 0;
iframe.height = 0;
document.body.appendChild(iframe);
}
if (e && e.type == "onload" && h) {
b.parentNode.removeChild(b);
b = null;
}
if (!b) {
b = document.createElement("div");
b.id = divId;
b.style.cssText = "clear:both;"
document.body.appendChild(b);
}
var l = b.offsetTop + b.offsetHeight;
iframe.src = (decodeURIComponent(f) || "https://*****/test2") + "&h=" + l + "&ifmID=" + (h || "ifm") + "&cbn=test" + "&mh=" + g + "&t=" + ( (+ new Date()));
if (e && e.type =="onload") {
window.onload = null;
}
}
window.onload = function() {
ifrH({type: "onload"});
}
// ifrH();
$('button').click(function(){
$('div').show();
ifrH();
})
})
</script>
</body>
</html>
<script>
var search = window.location.search,
getSearchParam = function (search, key) {
var mc = search.match (new RegExp ( key + "=([^\&]*)" ) ), ret="";
mc && mc.length && (ret = mc[0].replace( key + "=",""));
return ret;
},
// 參數h
h = getSearchParam(search,"h"),
ifmID = getSearchParam(search,"ifmID"),
cbn = getSearchParam(search,"cbn"),
// 寬高
mh = getSearchParam(search,"mh") || h,
isFunction = function(fn){
return !!fn && !fn.nodeName && fn.constructor != String && fn.constructor != RegExp && fn.constructor != Array && (/function/i).test(fn + "");
};
try{
if(parent && parent.parent){
ifm = parent.parent.document.getElementById(ifmID);
ifm && mh && (ifm.height=mh);
fn=parent.parent[cbn];
isFunction(fn) && fn.call();
ifm=null;
}
}catch(ex){
console.log(ex);
}
</script>
透過 B、C 兩個中轉 iframe 的接力,就繞過了同源策略的限制,實現了跨主域名稱下的自動高度調整。