我在一頁面設置一iframe,想讓iframe的高度能夠根據src源的頁面的高度而自適應,我用contentDocument來獲得iframe的document,可是當我的頁面的域和iframe不同時谷歌他會報安全錯誤,而無法正確的獲取到iframe的document,應該如何解決這個問題;例如下面的例子樣:我的父頁面在http://hij.kmn.net:8088/demo....<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Document</title></head><body> <iframe src="http://abc.efg.net:8088/" frameborder="0" id="iframepage" onLoad=iFrameHeight()></iframe></body> <script type="text/javascript"> function iFrameHeight() { var ifm= document.getElementById("iframepage"); var subWeb = document.frames ? document.frames["iframepage"].document : ifm.contentDocument; console.log(ifm,subWeb)if(ifm != null && subWeb != null) { ifm.height = subWeb.body.scrollHeight; ifm.width = subWeb.body.scrollWidth;} } ;</script></html>就會報錯:Uncaught SecurityError: Blocked a frame with origin "http://hij.kmn.net:8088/" from accessing a frame with origin "http://abc.efg.net:8088/". Protocols, domains, and ports must match.這種情況該怎樣解決,如同一域下面是可以正常的!就是不同域下有什么方法解決這個問題沒?
怎么解決iframe自適應?
弒天下
2018-09-13 14:09:47