js操作iframe的一些方法介紹
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對象。但經(jīng)過測試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.dbjr.com.cn/index.php';
ifr.onload = function() {
alert('loaded');
};
document.body.appendChild(ifr);
但是ie卻又似乎提供了onload事件,下面兩種方法都會觸發(fā)onload
方法一:
<iframe onload="alert('loaded');" src="http://www.dbjr.com.cn/index.php"></iframe>
方法二:
//只有ie才支持為createElement傳遞這樣的參數(shù)
var ifr = document.createElement('<iframe onload="alert('loaded');" src="http://www.dbjr.com.cn/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
- js動態(tài)創(chuàng)建上傳表單通過iframe模擬Ajax實現(xiàn)無刷新
- iframe子父頁面調(diào)用js函數(shù)示例
- js與jQuery 獲取父窗、子窗的iframe
- js下判斷 iframe 是否加載完成的完美方法
- js iframe跨域訪問(同主域/非同主域)分別深入介紹
- js中iframe調(diào)用父頁面的方法
- js實現(xiàn)網(wǎng)頁防止被iframe框架嵌套及幾種location.href的區(qū)別
- JS中Iframe之間傳值及子頁面與父頁面應用
- js實現(xiàn)iframe動態(tài)調(diào)整高度的代碼
- JavaScript實現(xiàn)同一頁面內(nèi)兩個表單互相傳值的方法
- jquery ajax提交表單從action傳值到jsp實現(xiàn)小結
- JS實現(xiàn)向iframe中表單傳值的方法
相關文章
基于JS實現(xiàn)帶并發(fā)限制的異步調(diào)度器
這篇文章主要為大家詳細介紹了如何基于JS實現(xiàn)帶并發(fā)限制的異步調(diào)度器,文中的示例代碼講解詳細,具有一定的學習價值,需要的可以參考下2023-05-05javascript數(shù)組按屬性分組實現(xiàn)方法
在開發(fā)過程中,前端有時需要對后端返回的數(shù)據(jù)進行一些處理,當后端返回給我們json對象數(shù)組時,我們可能會需要按照對象中的某一個屬性來進行分組,下面這篇文章主要給大家介紹了關于javascript數(shù)組按屬性分組的實現(xiàn)方法,需要的朋友可以參考下2023-05-05ES6基礎之解構賦值(destructuring assignment)
這篇文章主要介紹了ES6基礎之解構賦值(destructuring assignment),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02JavaScript必知必會(三) String .的方法來自何方
這篇文章主要介紹了JavaScript必知必會(三) String .的方法來自何方的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06javascript實現(xiàn)跳轉(zhuǎn)菜單的具體方法
這篇文章介紹了javascript實現(xiàn)跳轉(zhuǎn)菜單的具體方法,有需要的朋友可以參考一下2013-07-07