window.parent與window.openner區(qū)別介紹
更新時間:2012年04月12日 16:22:20 作者:
今天總結(jié)一下js中幾個對象的區(qū)別和用法,對這幾個概念混淆的朋友可以看看
今天總結(jié)一下js中幾個對象的區(qū)別和用法:
首先來說說 parent.window與top.window的用法
"window.location.href"、"location.href"是本頁面跳轉(zhuǎn)
"parent.location.href"是上一層頁面跳轉(zhuǎn)
"top.location.href"是最外層的頁面跳轉(zhuǎn)
舉例說明:
如果A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js這樣寫
"window.location.href"、"location.href":D頁面跳轉(zhuǎn)
"parent.location.href":C頁面跳轉(zhuǎn)
"top.location.href":A頁面跳轉(zhuǎn)
現(xiàn)在終于明白了連接的時候target的用法了:
_blank:重新打開一個窗口
_parent:父窗口執(zhí)行重定向
_self:自身頁面重定向
_top:第一個父窗口重定向
綜上所述可知:parent.window:父窗口對象 top.window:第一個父窗口的對象
下面來重點看看window.parent與window.openner區(qū)別
window.parent 是iframe頁面調(diào)用父頁面對象,當我們想從iframe內(nèi)嵌的頁面中訪問外層頁面是可以直接利用window.parent獲??;
例子如下:
A.html
<html>
<head>
<title>父頁面</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> 打開的子頁面調(diào)用父頁面對象
例子如下
a.html
<html>
<head>
<title>父頁面</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="打開窗口B" onclick="openB();" /><br />
<a href="b.html" target="_blank">超鏈接打開B頁面</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>
下面來舉幾個常用的例子:
parent.window與top.window一般在分割的頁面即 frameset或iframe中使用
注銷整個框架后返回到login.aspx:parent.window.location='Login.aspx'或者
top.window.location='Login.aspx'
window.parent也是常在框架中使用,
刷新:window.parent.location.reload();或者刷新某個框架:window.parent.MainForm.location.reload();
獲得其他框架的元素值:window.parent.MainForm.form1.text1.value;
window.opener主要是獲得通過超鏈接或者 window.open() 打開本身頁面的頁面的一些,比如獲得值,刷新等
刷新:window.opener.location.reload();
獲值:window.opener.document.getElement("txtName").value;
后退:top.playFrame.history.go(-1);
前進: top.playFrame.history.go(1);
刷新: top.playFrame.location.reload();
就總結(jié)到這里,這些對象很實用
首先來說說 parent.window與top.window的用法
"window.location.href"、"location.href"是本頁面跳轉(zhuǎn)
"parent.location.href"是上一層頁面跳轉(zhuǎn)
"top.location.href"是最外層的頁面跳轉(zhuǎn)
舉例說明:
如果A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js這樣寫
"window.location.href"、"location.href":D頁面跳轉(zhuǎn)
"parent.location.href":C頁面跳轉(zhuǎn)
"top.location.href":A頁面跳轉(zhuǎn)
現(xiàn)在終于明白了連接的時候target的用法了:
_blank:重新打開一個窗口
_parent:父窗口執(zhí)行重定向
_self:自身頁面重定向
_top:第一個父窗口重定向
綜上所述可知:parent.window:父窗口對象 top.window:第一個父窗口的對象
下面來重點看看window.parent與window.openner區(qū)別
window.parent 是iframe頁面調(diào)用父頁面對象,當我們想從iframe內(nèi)嵌的頁面中訪問外層頁面是可以直接利用window.parent獲??;
例子如下:
A.html
復制代碼 代碼如下:
<html>
<head>
<title>父頁面</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> 打開的子頁面調(diào)用父頁面對象
例子如下
a.html
復制代碼 代碼如下:
<html>
<head>
<title>父頁面</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="打開窗口B" onclick="openB();" /><br />
<a href="b.html" target="_blank">超鏈接打開B頁面</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>
下面來舉幾個常用的例子:
parent.window與top.window一般在分割的頁面即 frameset或iframe中使用
注銷整個框架后返回到login.aspx:parent.window.location='Login.aspx'或者
top.window.location='Login.aspx'
window.parent也是常在框架中使用,
刷新:window.parent.location.reload();或者刷新某個框架:window.parent.MainForm.location.reload();
獲得其他框架的元素值:window.parent.MainForm.form1.text1.value;
window.opener主要是獲得通過超鏈接或者 window.open() 打開本身頁面的頁面的一些,比如獲得值,刷新等
刷新:window.opener.location.reload();
獲值:window.opener.document.getElement("txtName").value;
后退:top.playFrame.history.go(-1);
前進: top.playFrame.history.go(1);
刷新: top.playFrame.location.reload();
就總結(jié)到這里,這些對象很實用
您可能感興趣的文章:
相關(guān)文章
ECMAScript5(ES5)中bind方法使用小結(jié)
這篇文章主要介紹了ECMAScript5(ES5)中bind方法使用小結(jié),bind和call以及apply一樣,都是可以改變上下文的this指向的,需要的朋友可以參考下2015-05-05一個簡單的網(wǎng)站訪問JS計數(shù)器 刷新1次加1次訪問
一個簡單的網(wǎng)站訪問JS計數(shù)器,一般就是學習下原來,不建議使用,現(xiàn)在cnzz或百度統(tǒng)計多試不錯的2012-09-09javascript的原生方法獲取數(shù)組中的最大(最小)值
獲取一個數(shù)組中的最大(最?。┲档淖詈唵蔚姆椒ǎ褪菍?shù)組進行一次遍歷,通過比較,找到其最大(最?。┲怠5瞧鋵嵲趈avascript的原生方法中,已經(jīng)提供了一些快捷方法,可以實現(xiàn)此功能,需要的朋友可以了解下2012-12-12Javascript 動態(tài)改變imput type屬性
這篇文章主要介紹了Javascript 動態(tài)改變imput type屬性的相關(guān)資料,并附簡單實例代碼,需要的朋友可以參考下2016-11-11