js操作iframe的一些方法介紹

來源:互聯網
上載者:User

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

chrome:iframeElement. contentWindow
firefox: iframeElement.contentWindow
ie6: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;  

2. 獲得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;  
};  

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

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

4. 獲得iframe在父頁面中的html標籤
存在跨域訪問限制。

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

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

(javascript)
複製代碼 代碼如下:
var ifr = document.createElement('iframe');  
ifr.src = 'http://www.jb51.net/index.php';  
ifr.onload = function() {  
    alert('loaded');  
};  
document.body.appendChild(ifr);  

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

方法一:
複製代碼 代碼如下:
<iframe  onload="alert('loaded');"  src="http://www.jb51.net/index.php"></iframe>  
  
方法二:  
//只有ie才支援為createElement傳遞這樣的參數
複製代碼 代碼如下:
var ifr = document.createElement('<iframe  onload="alert('loaded');" src="http://www.jb51.net/index.php"></iframe>');  
document.body.appendChild(ifr);  

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

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

複製代碼 代碼如下:
var ifr = document.createElement('iframe');  
ifr.src = 'http://b.jb51.net/b.php';  
if (ifr.attachEvent) {  
    ifr.attachEvent('onload',  function(){ alert('loaded'); });  
} else {  
    ifr.onload  = function() { alert('loaded'); };  
}  
document.body.appendChild(ifr);  

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

複製代碼 代碼如下:
var ifr1 = document.getElementById('ifr1');  
var ifr1win = window.frames[0];  
ifr1win.frameElement === ifr1;   // true  
ifr1win === ifr1;    // false  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.