window.parent與window.openner區(qū)別介紹
更新時(shí)間:2012年04月12日 16:22:20 作者:
今天總結(jié)一下js中幾個(gè)對(duì)象的區(qū)別和用法,對(duì)這幾個(gè)概念混淆的朋友可以看看
今天總結(jié)一下js中幾個(gè)對(duì)象的區(qū)別和用法:
首先來(lái)說(shuō)說(shuō) parent.window與top.window的用法
"window.location.href"、"location.href"是本頁(yè)面跳轉(zhuǎn)
"parent.location.href"是上一層頁(yè)面跳轉(zhuǎn)
"top.location.href"是最外層的頁(yè)面跳轉(zhuǎn)
舉例說(shuō)明:
如果A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js這樣寫(xiě)
"window.location.href"、"location.href":D頁(yè)面跳轉(zhuǎn)
"parent.location.href":C頁(yè)面跳轉(zhuǎn)
"top.location.href":A頁(yè)面跳轉(zhuǎn)
現(xiàn)在終于明白了連接的時(shí)候target的用法了:
_blank:重新打開(kāi)一個(gè)窗口
_parent:父窗口執(zhí)行重定向
_self:自身頁(yè)面重定向
_top:第一個(gè)父窗口重定向
綜上所述可知:parent.window:父窗口對(duì)象 top.window:第一個(gè)父窗口的對(duì)象
下面來(lái)重點(diǎn)看看window.parent與window.openner區(qū)別
window.parent 是iframe頁(yè)面調(diào)用父頁(yè)面對(duì)象,當(dāng)我們想從iframe內(nèi)嵌的頁(yè)面中訪問(wèn)外層頁(yè)面是可以直接利用window.parent獲??;
例子如下:
A.html
<html>
<head>
<title>父頁(yè)面</title>
</head>
<body>
<form id="form1" action="">
<div>
輸入值:
<input type="text" name="username" id="username" /><br />
<iframe src="b.html" width="400px" height="300px"></iframe>
</div>
</form>
</body>
</html>
B.html
<html>
<head>
<script type="text/javascript">
function getpValue()
{
document.getElementByIdx_x_x_x("span1").innerText=window.parent.document.getElementByIdx_x_x_x("username").value;
}
</script>
</head>
<body>
<span>文本框值為:</span><span id="span1"></span><br />
<input type="button" value="獲取父窗口內(nèi)的文本框值" onclick="getpValue();">
</body>
</html>
window.opener 是window.open或超鏈接<a> 打開(kāi)的子頁(yè)面調(diào)用父頁(yè)面對(duì)象
例子如下
a.html
<html>
<head>
<title>父頁(yè)面</title>
<script type="text/javascript">
function openB()
{
window.open('b.html','b','width=400,height=200,status=no,toolbar=no,menubar=no,location=no,resizable=yes,left=200,top=100');
}
</script>
</head>
<body>
<form id="form1" action="">
<div>
輸入值:
<input type="text" name="username" id="username" /><br />
<input type="button" value="打開(kāi)窗口B" onclick="openB();" /><br />
<a href="b.html" target="_blank">超鏈接打開(kāi)B頁(yè)面</a>
</div>
</form>
</body>
</html>
b.html
<html>
<head>
<script type="text/javascript">
function getpValue()
{
document.getElementByIdx_x_x_x("span1").innerText=window.opener.document.getElementByIdx_x_x_x("username").value;
}
</script>
</head>
<body>
<span>文本框值為:</span><span id="span1"></span><br />
<input type="button" value="獲取父窗口內(nèi)的文本框值" onclick="getpValue();">
</body>
</html>
下面來(lái)舉幾個(gè)常用的例子:
parent.window與top.window一般在分割的頁(yè)面即 frameset或iframe中使用
注銷(xiāo)整個(gè)框架后返回到login.aspx:parent.window.location='Login.aspx'或者
top.window.location='Login.aspx'
window.parent也是常在框架中使用,
刷新:window.parent.location.reload();或者刷新某個(gè)框架:window.parent.MainForm.location.reload();
獲得其他框架的元素值:window.parent.MainForm.form1.text1.value;
window.opener主要是獲得通過(guò)超鏈接或者 window.open() 打開(kāi)本身頁(yè)面的頁(yè)面的一些,比如獲得值,刷新等
刷新:window.opener.location.reload();
獲值:window.opener.document.getElement("txtName").value;
后退:top.playFrame.history.go(-1);
前進(jìn): top.playFrame.history.go(1);
刷新: top.playFrame.location.reload();
就總結(jié)到這里,這些對(duì)象很實(shí)用
首先來(lái)說(shuō)說(shuō) parent.window與top.window的用法
"window.location.href"、"location.href"是本頁(yè)面跳轉(zhuǎn)
"parent.location.href"是上一層頁(yè)面跳轉(zhuǎn)
"top.location.href"是最外層的頁(yè)面跳轉(zhuǎn)
舉例說(shuō)明:
如果A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js這樣寫(xiě)
"window.location.href"、"location.href":D頁(yè)面跳轉(zhuǎn)
"parent.location.href":C頁(yè)面跳轉(zhuǎn)
"top.location.href":A頁(yè)面跳轉(zhuǎn)
現(xiàn)在終于明白了連接的時(shí)候target的用法了:
_blank:重新打開(kāi)一個(gè)窗口
_parent:父窗口執(zhí)行重定向
_self:自身頁(yè)面重定向
_top:第一個(gè)父窗口重定向
綜上所述可知:parent.window:父窗口對(duì)象 top.window:第一個(gè)父窗口的對(duì)象
下面來(lái)重點(diǎn)看看window.parent與window.openner區(qū)別
window.parent 是iframe頁(yè)面調(diào)用父頁(yè)面對(duì)象,當(dāng)我們想從iframe內(nèi)嵌的頁(yè)面中訪問(wèn)外層頁(yè)面是可以直接利用window.parent獲??;
例子如下:
A.html
復(fù)制代碼 代碼如下:
<html>
<head>
<title>父頁(yè)面</title>
</head>
<body>
<form id="form1" action="">
<div>
輸入值:
<input type="text" name="username" id="username" /><br />
<iframe src="b.html" width="400px" height="300px"></iframe>
</div>
</form>
</body>
</html>
B.html
復(fù)制代碼 代碼如下:
<html>
<head>
<script type="text/javascript">
function getpValue()
{
document.getElementByIdx_x_x_x("span1").innerText=window.parent.document.getElementByIdx_x_x_x("username").value;
}
</script>
</head>
<body>
<span>文本框值為:</span><span id="span1"></span><br />
<input type="button" value="獲取父窗口內(nèi)的文本框值" onclick="getpValue();">
</body>
</html>
window.opener 是window.open或超鏈接<a> 打開(kāi)的子頁(yè)面調(diào)用父頁(yè)面對(duì)象
例子如下
a.html
復(fù)制代碼 代碼如下:
<html>
<head>
<title>父頁(yè)面</title>
<script type="text/javascript">
function openB()
{
window.open('b.html','b','width=400,height=200,status=no,toolbar=no,menubar=no,location=no,resizable=yes,left=200,top=100');
}
</script>
</head>
<body>
<form id="form1" action="">
<div>
輸入值:
<input type="text" name="username" id="username" /><br />
<input type="button" value="打開(kāi)窗口B" onclick="openB();" /><br />
<a href="b.html" target="_blank">超鏈接打開(kāi)B頁(yè)面</a>
</div>
</form>
</body>
</html>
b.html
復(fù)制代碼 代碼如下:
<html>
<head>
<script type="text/javascript">
function getpValue()
{
document.getElementByIdx_x_x_x("span1").innerText=window.opener.document.getElementByIdx_x_x_x("username").value;
}
</script>
</head>
<body>
<span>文本框值為:</span><span id="span1"></span><br />
<input type="button" value="獲取父窗口內(nèi)的文本框值" onclick="getpValue();">
</body>
</html>
下面來(lái)舉幾個(gè)常用的例子:
parent.window與top.window一般在分割的頁(yè)面即 frameset或iframe中使用
注銷(xiāo)整個(gè)框架后返回到login.aspx:parent.window.location='Login.aspx'或者
top.window.location='Login.aspx'
window.parent也是常在框架中使用,
刷新:window.parent.location.reload();或者刷新某個(gè)框架:window.parent.MainForm.location.reload();
獲得其他框架的元素值:window.parent.MainForm.form1.text1.value;
window.opener主要是獲得通過(guò)超鏈接或者 window.open() 打開(kāi)本身頁(yè)面的頁(yè)面的一些,比如獲得值,刷新等
刷新:window.opener.location.reload();
獲值:window.opener.document.getElement("txtName").value;
后退:top.playFrame.history.go(-1);
前進(jìn): top.playFrame.history.go(1);
刷新: top.playFrame.location.reload();
就總結(jié)到這里,這些對(duì)象很實(shí)用
您可能感興趣的文章:
相關(guān)文章
Javascript入門(mén)學(xué)習(xí)第六篇 js DOM編程
上篇文章納悶的問(wèn)題,將在這章和以后的幾章里,慢慢搞定。 從今天起,開(kāi)始學(xué)習(xí)DOM編程 讓我們慢慢稱(chēng)為一名初級(jí)的js程序員。 然后往js匠人方向發(fā)展。2008-07-07
關(guān)于JavaScript對(duì)象類(lèi)型之Array及Object
這篇文章主要介紹了關(guān)于JavaScript對(duì)象類(lèi)型之Array及Object,Array 類(lèi)型是 ECMAScript 中最常用的類(lèi)型了。而且,ECMAScript 中的數(shù)組與其他多數(shù)語(yǔ)言中的數(shù)組有著相當(dāng)大的區(qū)別,需要的朋友可以參考下2023-05-05
對(duì)new functionName()定義一個(gè)函數(shù)的理解
這篇文章主要介紹了對(duì)new functionName()定義一個(gè)函數(shù)的理解,需要的朋友可以參考下2014-05-05
在javascript中對(duì)于DOM的加強(qiáng)
本篇文章,小編為大家介紹關(guān)于在javascript中對(duì)于DOM的加強(qiáng),有需要的朋友可以參考一下2013-04-04
JavaScript基礎(chǔ)心法 深淺拷貝(淺拷貝和深拷貝)
淺拷貝和深拷貝都是對(duì)于JS中的引用類(lèi)型而言的,淺拷貝就只是復(fù)制對(duì)象的引用,如果拷貝后的對(duì)象發(fā)生變化,原對(duì)象也會(huì)發(fā)生變化。只有深拷貝才是真正地對(duì)對(duì)象的拷貝2018-03-03

