利用JS對iframe父子(內外)頁面進行操作的方法教程,jsiframe

來源:互聯網
上載者:User

利用JS對iframe父子(內外)頁面進行操作的方法教程,jsiframe

本文主要給大家介紹了關於利用JS對iframe父子(內外)頁面進行操作的方法,分享出來供大家參考學習,下面來一起看看詳細的介紹:

一、擷取iframe裡的內容

在開始之前,首先我們來看看如何擷取iframe裡的內容,擷取iframe中內容主要的兩個API就是contentWindow,和contentDocument iframe.contentWindow, 擷取iframe的window對象 iframe.contentDocument, 擷取iframe的document對象 這兩個API只是DOM節點提供的方式(即getELement系列對象)

 var iframe = document.getElementById("iframe1"); var iwindow = iframe.contentWindow; var idoc = iwindow.document;  console.log("window",iwindow);//擷取iframe的window對象  console.log("document",idoc); //擷取iframe的document  console.log("html",idoc.documentElement);//擷取iframe的html  console.log("head",idoc.head); //擷取head  console.log("body",idoc.body); //擷取body

實際情況如:

另外更簡單的方式是,結合Name屬性,通過window提供的frames擷取.

<iframe src ="/index.html" id="ifr1" name="ifr1" scrolling="yes"> <p>Your browser does not support iframes.</p></iframe><script type="text/javascript"> console.log(window.frames['ifr1'].window);console.dir(document.getElementById("ifr1").contentWindow);</script>

其實window.frames[‘ifr1']返回的就是window對象,即

window.frames['ifr1']===window

這裡就看你想用哪一種方式擷取window對象,兩者都行,不過本人更傾向於第二種使用frames[xxx].因為,字母少啊喂~ 然後,你就可以操控iframe裡面的DOM內容。

二、在iframe中擷取父級內容

同理,在同域下,父頁面可以擷取子iframe的內容,那麼子iframe同樣也能操作父頁面內容。在iframe中,可以通過在window上掛載的幾個API進行擷取.

  • window.parent 擷取上一級的window對象,如果還是iframe則是該iframe的window對象
  • window.top 擷取最頂級容器的window對象,即,就是你開啟頁面的文檔
  • window.self 返回自身window的引用。可以理解 window===window.self(腦殘)

:

擷取了之後,我們就可以進行相關操作了。 在同域的iframe中,我們可以巧妙的使用iframe的黑科技來實現一些trick.

iframe的輪詢

話說在很久很久以前,我們實現非同步發送請求是使用iframe實現的~! 怎麼可能!!! 真的史料為證(自行google), 那時候為了不跳轉頁面,提交表單時是使用iframe提交的。現在,前端發展尼瑪真快,websocket,SSE,ajax等,逆天skill的出現,顛覆了iframe, 現在基本上只能活在IE8,9的瀏覽器內了。 但是,寶寶以為這樣就可以不用瞭解iframe了,而現實就是這麼殘酷,我們目前還需要相容IE8+。所以,iframe 實現長輪詢和長串連的trick 我們還是需要涉獵滴。

iframe長輪詢

如果寫過ajax的童鞋,應該知道,長輪詢就是在ajax的readyState = 4的時,再次執行原函數即可。 這裡使用iframe也是一樣,非同步建立iframe,然後reload, 和後台協商好, 看後台哥哥們將返回的資訊放在,然後擷取裡面資訊即可. 這裡是直接放在body裡.

var iframeCon = docuemnt.querySelector('#container'), text; //傳遞的資訊 var iframe = document.createElement('iframe'), iframe.id = "frame", iframe.style = "display:none;", iframe.name="polling", iframe.src="target.html"; iframeCon.appendChild(iframe); iframe.onload= function(){ var iloc = iframe.contentWindow.location, idoc = iframe.contentDocument; setTimeout(function(){ text = idoc.getElementsByTagName('body')[0].textContent; console.log(text); iloca.reload(); //重新整理頁面,再次擷取資訊,並且會觸發onload函數 },2000); }

這樣就可以實現ajax的長輪詢的效果。 當然,這裡只是使用reload進行擷取,你也可以添加iframe和刪除iframe的方式,進行發送資訊,這些都是根據具體情境應用的。另外在iframe中還可以實現非同步載入js檔案,不過,iframe和首頁是共用串連池的,所以還是很蛋疼的,現在基本上都被XHR和hard calllback取締了,這裡也不過多介紹了。

1.js在iframe子頁面操作父頁面元素代碼:

window.parent.document.getElementByIdx_x("父頁面元素id");

2.js在父頁面擷取iframe子頁面元素代碼如下:

window.frames["iframe_ID"].document.getElementByIdx_x("子頁面元素id");

3. jquery在iframe子頁面擷取父頁面元素代碼如下:

$("#objid",parent.document)

4. jquery在父頁面擷取iframe子頁面的元素

$("#objid",document.frames('iframename').document)

5.在iframe中調用父頁面中定義的方法和變數:

window.parent.window.parentMethod();window.parent.window.parentValue;

6.在父頁面操作iframe子頁面的方法和變數

window.frames["iframe_ID"].window.childMethod();window.frames["iframe_ID"].window.childValue;

一、同域下父子頁面的通訊

父頁面parent.html

<html><head> <script type="text/javascript">  function say(){   alert("parent.html");  }  function callChild(){   myFrame.window.say();   myFrame.window.document.getElementById("button").value="調用結束";  } </script></head><body> <input id="button" type="button" value="調用child.html中的函數say()" onclick="callChild()"/> <iframe name="myFrame" src="http://caibaojian.com/child.html"></iframe></body></html>

子頁面child.html

<html><head> <script type="text/javascript">  function say(){   alert("child.html");  }  function callParent(){   parent.say();   parent.window.document.getElementById("button").value="調用結束";  } </script></head><body> <input id="button" type="button" value="調用parent.html中的say()函數" onclick="callParent()"/></body></html>

注意事項

要確保在iframe載入完成後再進行操作,如果iframe還未載入完成就開始調用裡面的方法或變數,會產生錯誤。判斷iframe是否載入完成有兩種方法:

      1. iframe上用onload事件

      2. 用document.readyState=="complete"來判斷

二、跨域父子頁面通訊方法

如果iframe所連結的是外部頁面,因為安全機制就不能使用同網域名稱下的通訊方式了。

1.父頁面向子頁面傳遞資料

實現的技巧是利用location對象的hash值,通過它傳遞通訊資料。在父版面設定iframe的src後面多加個data字串,然後在子頁面中通過某種方式能即時的擷取到這兒的data就可以了,例如:

1.1 在子頁面中通過setInterval方法設定定時器,監聽location.href的變化即可獲得上面的data資訊

1.2. 然後子頁面根據這個data資訊進行相應的邏輯處理

2.子頁面向父頁面傳遞資料

實現技巧就是利用一個代理iframe,它嵌入到子頁面中,並且和父頁面必須保持是同域,然後通過它充分利用上面第一種通訊方式的實現原理就把子頁面的資料傳遞給代理iframe,然後由於代理的iframe和首頁面是同域的,所以首頁面就可以利用同域的方式擷取到這些資料。使用 window.top或者window.parent.parent擷取瀏覽器最頂層window對象的引用。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的協助,如有疑問大家可以留言交流,謝謝大家對幫客之家的支援。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.