js控制父子頁面傳值(iframe和window.open)


在html中,window對象代表瀏覽器中一個打開的窗口,就像我們C/S中做窗體一樣,在該頁的window對象就是new了一個新的窗體對象。

就像做C/S開發一樣,瀏覽器是一個軟件,每一個網頁都是它new的一個對象,對象與對象的信息交流只能通過方法(前提是在自己的對象域中,當然如果兩個對象在同一個作用域中,就不用那么麻煩了,但是每一個網頁都是一個對象,在本對象內部作用域去訪問另一個對象,就需要有到公共的方法,並且兩個對象都可以接受到的。就是在后台才會有的request和response等方法)。

所以本節討論的:主要是在窗體自己作用域內父子頁面傳值,有兩點

  1. iframe是html中的一個標簽,也就是window對象的一個成員(盡管它是網頁),對象與成員之間的信息傳遞是簡單的
  2. js腳本window.open(網頁路徑),就是window對象new了一個window對象,open實際也是window對象的成員,但是它與iframe不同的是它是一個匿名對象。

頁面傳值方法:

1. iframe

             子頁面獲取父頁面對象方法:window.parent

             父頁面獲取子頁面對象方法:window.frames[iframe對象的name值]

2. window.open()

             子頁面獲取父頁面對象方法: window.opener

參考頁面:http://www.jb51.net/article/25629.htm

代碼參考:

1.父頁面代碼(father.html)

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 5     <script src="../js/jquery-1.10.2.js"></script>
 6     <title></title>
 7     <script type="text/javascript">
 8         function sayOpen()
 9         {
10             alert("I am father(設置或獲取創建當前窗口的窗口的引用)");
11         }
12         function sayFrame() {
13             alert("I am father(獲取對象層次中的父窗口)");
14             
15         }
16         function getIframe()
17         {
18             window.frames["one"].showSon();
19             window.frames["two"].showIframe();
20         }
21     </script>
22 </head>
23 <body>
24     該頁面是被創建的頁面:<a href="#" onclick="window.open('son.html')">創建子頁</a><br />
25     <br />
26     <input type="button" onclick="getIframe()" value="獲取子頁" />
27     <br />
28     <iframe name="one" frameborder="0" src="son.html">
29     </iframe>
30     <iframe name="two" frameborder="0" src="iframe_one.html">
31     </iframe>
32 </body>
33 </html>
View Code

2.子頁面代碼

(son.html和iframe_one.html)

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 5     <title></title>
 6     <script type="text/javascript">
 7         function showSon() {
 8             alert("I am son");
 9         }
10     </script>
11 </head>
12 <body>
13     <input type="button" onclick="window.opener.sayOpen()" value="創建頁單擊事件" /><br />
14     <input type="button" onclick="window.parent.sayFrame()" value="Iframe頁單擊事件" /><br />
15     
16 </body>
17 </html>
View Code
 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 5     <title></title>
 6 
 7     <script type="text/javascript">
 8         function showIframe() {
 9             alert("I am iframe");
10         }
11     </script>
12 
13 </head>
14 <body>
15     我是子頁iframe
16 </body>
17 </html>
View Code

iframe標簽常用設置:

<iframe height="300px" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes" src="../../../EFM/Model.aspx" style="overflow-x:visible;overflow-y:visible"></iframe> 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM