當前位置:首頁 » 密碼管理 » js跨域訪問iframe

js跨域訪問iframe

發布時間: 2023-04-15 04:31:43

1. 如何用javascript 跨域獲取iframe子頁面的元素信息

跨域的話想用父頁面的js獲取iframe里的元素是不可能的。
用chrome擴展程序的話是可以的。

2. JS跨域訪問操作iframe代碼

父頁里有一個層<div>,<div>里有一個子框架<iframe>,<iframe>的屬性id="mainFrame"

例如:

父頁代碼:
<html>
<body>
<div id="mainDiv" style="display:none; width:550px; height:200px" align="left">
<iframe name="mainFrame" src="./AllLanguageAction.do" frameborder="0" scrolling="no" width="550" height=250 id="mainFrame">
</iframe>
</div>
</body>
</html>

子頁中代碼:
<html>
<head>
<script type="text/javascript">
function IFrameResize()
{
var obj = parent.document.getElementById("mainFrame");
obj.height = this.document.body.scrollHeight;
}
</script>
</head>
<body onload="IFrameResize();">
</body>
</html>

3. js操作iframe的一些方法介紹

獲得iframe的window對象 存在跨域訪問限制

chrome iframeElement contentWindow firefox iframeElement contentWindow ie iframeElement contentWindow

文章Iframes onload and document domain中說「he iframe element object has a property called contentDocument that contains the iframe s document object so you can use the parentWindow property to retrieve the window object 」意思就是一些瀏覽器可以通過iframeElement contentDocument parentWindow獲得iframe的 window對象 但經過測試firefox chrome的element contentDocument對象沒有parentWindow屬性

(javascript)

復制代碼 代碼如下: function getIframeWindow(element){ return element contentWindow; //return element contentWindow || element contentDocument parentWindow; }

獲得iframe的document對象 存在跨域訪問限制

chrome iframeElement contentDocument firefox iframeElement contentDocument ie element contentWindow document 備注 ie沒有iframeElement contentDocument屬性

(javascript)

復制代碼 代碼如下: var getIframeDocument = function(element) { return element contentDocument || element contentWindow document; };

iframe中獲得父頁面的window對象 存在跨域訪問限制

父頁面 window parent 頂層頁面 window top 適用於所有瀏覽器

獲得iframe在父頁面中的標簽 存在跨域訪問限制

window frameElement(類型 HTMLElement) 適用於所有瀏覽器

iframe的onload事件 非ie瀏覽器都提供了onload事件 例如下面代碼在ie中是不會有彈出框的

(javascript)

復制代碼 代碼如下: var ifr = document createElement( iframe ); ifr src = ; ifr onload = function() { alert( loaded ); }; document body appendChild(ifr);

但是ie卻又似乎提供了onload事件 下面兩種方法都會觸發onload

方法一

復制代碼 代碼如下: <iframe onload="alert( loaded );" src="

方法二 //只有ie才支持為createElement傳遞這樣的參數

復制代碼 代碼如下: var ifr = document createElement( <iframe onload="alert( loaded );" src=" ); document body appendChild(ifr);

由於iframe元素包含於父級頁面中 因此以上方法均不存在跨域問題

實際上IE提供了onload事件 但必須使用attachEvent進行綁定

復制代碼 代碼如下: var ifr = document createElement( iframe ); ifr src = ; if (ifr attachEvent) { ifr attachEvent( onload function(){ alert( loaded ); }); } else { ifr onload = function() { alert( loaded ); }; } document body appendChild(ifr);

frames window frames可以取到頁面中的幀(iframe frame等) 需要注意的是取到的是window對象 而不是HTMLElement

復制代碼 代碼如下: lishixin/Article/program/Java/JSP/201311/19939

熱點內容
三質數編程 發布:2025-09-17 04:04:41 瀏覽:553
ftp伺服器密碼如何修改 發布:2025-09-17 03:48:07 瀏覽:297
python爬蟲豆瓣 發布:2025-09-17 03:41:59 瀏覽:204
我的世界國際版加入伺服器電腦版 發布:2025-09-17 03:41:14 瀏覽:934
附件上傳相片 發布:2025-09-17 03:39:53 瀏覽:437
微信賬號密碼組合是什麼 發布:2025-09-17 03:30:05 瀏覽:394
windows如何查看ftp安裝環境 發布:2025-09-17 03:30:02 瀏覽:912
java培訓機構推薦 發布:2025-09-17 03:20:58 瀏覽:87
開伺服器時如何開pvp 發布:2025-09-17 03:02:23 瀏覽:970
安卓手機序列號a開頭什麼意思 發布:2025-09-17 02:59:26 瀏覽:690