js实现iframe自适应子页面的高度(参考funoob.com)

<!DOCTYPE html>
<html>
<head>
<script>
function changeStyle()
{
var x=document.getElementById("myframe");
var y=(x.contentWindow || x.contentDocument);
if (y.document)
y=y.document;
//document.body.style.backgroundColor=y.body.style.backgroundColor; //获取子页面背景色
x.height=y.body.scrollHeight; //设置iframe自适应子页面的高度
}
</script>
</head>
<body>
//src="a.html"  本地测试 会报错
//错误信息--- Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

//问题解答参考:http://zhidao.baidu.com/link?url=joBv39wGjcZsnu_U4yGjqFwNwPORjZKRpqRtyI4W4UeoGEkdTekQj7kjyGWlPiNdyWlOYE9Lzi5rfMx039sPQeYjMQC3MygXRoLndZhJhhe
//跨页面操作涉及域的概念(origin)。错误的意思是:未捕获的安全错误:阻止了一个域为null的frame页面访问另一个域为null的页面。
<iframe id="myframe" src="a.html">  <p>Your browser does not support iframes.</p> </iframe> <br><br> <input type="button" onclick="changeStyle()" value="Change background color"> </body> </html>

 

posted on 2015-11-18 15:26  源人  阅读(245)  评论(0)    收藏  举报

导航