---
title: "iFrame 跨域高度自适应问题解决"
author: "xhgrid"
date: "2012-09-16T21:05:40Z"
tags: ["JavaScript"]
categories: ["Web前端"]
slug: "iframe"
draft: false
lastmod: "2012-09-12T16:51:57Z"
id: 1676981
url: "https://xhgrid.iteye.com/blog/1676981"
---

需求是：A页面(例如：www.taobao.com) 要嵌入B(例如： www.alibaba.com)，因为不能确定B页面的高度，所以要求高度自适应。 具体代码： iframe主页面 A.html

cross.js --cross放到父页面的域名下 复制代码 var inner = inner || {}; var getUrlValue = function (url) { var url = (url !== undefined) ? url : window.location.href; if (url.indexOf("#") &gt; -1) { var variable = url.split("#")\[1]; } else { var variable = url.split("?")\[1]; } if (variable === undefined) { return {}; } else { var value = {}; variable = variable.split("&"); for (var i = 0, m = variable.length; i &lt; m; i++) { value\[variable\[i].split("=")\[0]] = variable\[i].split("=")\[1]; } return value; } } var mathor\_url = getUrlValue()\['mathor\_url']; if (!mathor\_url) { if (/alibaba.com/.test(window.location.href.match(/\[^htps\\/]\*\[^\\/]+/g)\[1])) {---------判断是否是阿里巴巴的域名 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 &gt;= 0; i--) { var clink = links\[i]; 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