iFrame 跨域高度自适应问题解决
需求是:A页面(例如:www.taobao.com) 要嵌入B(例如: www.alibaba.com),因为不能确定B页面的高度,所以要求高度自适应。
具体代码:
iframe主页面 A.html
<iframe id="aFrame" scrolling='no' border=0 frameborder=0 width=100% height="400px" class="failure"
src="www.alibaba.com/B.html" runat="server">
cross.js --cross放到父页面的域名下 复制代码 var inner = inner || {}; var getUrlValue = function (url) { var url = (url !== undefined) ? url : window.location.href; if (url.indexOf("#") > -1) { var variable = url.split("#"); } else { var variable = url.split("?"); } if (variable === undefined) { return {}; } else { var value = {}; variable = variable.split("&"); for (var i = 0, m = variable.length; i < m; i++) { value.split("=")] = variable*.split("="); } return value; } } var mathor_url = getUrlValue(); if (!mathor_url) { if (/alibaba.com/.test(window.location.href.match(/*+/g))) {---------判断是否是阿里巴巴的域名 mathor_url = "www.taobao.com.cn/qiantao"; --------------要修改的A页面服务器地址(此地址是proxy放置地址的前段部分) }else{ mathor_url=" www.taobao.com.cn/qiantao ";------------ 要修改的A页面服务器地址 (可以是测试地址,或者是如果正式测试的都一样,则else可不要) } } inner = { iframe_el: null, url: 'http://' + mathor_url, aid: 1, href: null, time: null, signA: false, autoHeight: true, getDocHeight: function () { var height = (navigator.appName == "Microsoft Internet Explorer") ? document.body.scrollHeight + 20 : document.body.offsetHeight + 20; return height; }, postAction: function (u) { var fd = this; clearInterval(this.time); fd.iframe_el_new = document.createElement('iframe'); fd.iframe_el_new.height = 0; fd.iframe_el_new.style.height = '0px'; fd.iframe_el_new.style.width = '0px'; fd.iframe_el_new.style.border = 'none'; fd.iframe_el_new.width = 0; fd.iframe_el_new.frameborder = 0; fd.iframe_el_new.border = 0; fd.iframe_el_new.scrolling = 'no'; fd.iframe_el_new.src = fd.url + "/proxy.htm#aid=" + this.aid + "&" + u; fd.iframe_el.parentNode.appendChild(fd.iframe_el_new); fd.iframe_el.parentNode.removeChild(fd.iframe_el); fd.iframe_el = fd.iframe_el_new; this.aid++; }, setHeight: function (height) { if (height === undefined) { height = this.getDocHeight(); } this.postAction("action=setheight&height=" + height); }, start: function () { var fd = this; fd.iframe_el = document.getElementById("aFrame"); -----更改ifram的名称 if (this.autoHeight) { fd.old_height = 0; var autoHeight = function () { if (fd.old_height != inner.getDocHeight()) { fd.old_height = inner.getDocHeight(); fd.setHeight(); } } setInterval(autoHeight, 200); } } };
document.write('');-----此处也要进行修改,拼接proxy.htm的位置,注意proxy.htm存放的位置 inner.start(); (function () {if (!!window.ActiveXObject && !window.XMLHttpRequest) { var links = document.getElementsByTagName('a'); for (i = links.length - 1; i >= 0; i--) { var clink = links*; if (clink.target == "_top") { clink.onclick = (function () { var chref = clink.href; return function () { if (window.top) { window.top.location = chref; } } })() } } }
})()
复制代码
proxy.htm (代理页) --代理页面放到父页面的域名下 复制代码
最后将自适应的页面www.alibaba.com/B.html中添加引用cross.js脚本(放到之前)
http://www.cnblogs.com/liushanshan/archive/2011/05/19/2051094.html