今天總結一下js中幾個對象的區別和用法:
首先來說說 parent.window與top.window的用法
"window.location.href"、"location.href"是本頁面跳轉
"parent.location.href"是上一層頁面跳轉
"top.location.href"是最外層的頁面跳轉
舉例說明:
如果A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js這樣寫
"window.location.href"、"location.href":D頁面跳轉
"parent.location.href":C頁面跳轉
"top.location.href":A頁面跳轉
現在終於明白了連接的時候target的用法了:
_blank:重新打開一個窗口
_parent:父窗口執行重定向
_self:自身頁面重定向
_top:第一個父窗口重定向
綜上所述可知:parent.window:父窗口對象 top.window:第一個父窗口的對象
下面來重點看看window.parent與window.openner區別window.parent 是iframe頁面調用父頁面對象,當我們想從iframe內嵌的頁面中訪問外層頁面是可以直接利用window.parent獲取;
例子如下:
A.html
<html>
<head>
</head>
<body>
</body>
</html>
B.html
<html>
<head>
<script type="text/javascript">
</script>
</head>
<body>
<span>文本框值為:</span><span id="span1"></span><br />
<input type="button" value="獲取父窗口內的文本框值" onclick="getpValue();">
</body>
</html>
例子如下
a.html
<html>
<head>
</head>
<body>
</body>
</html>
b.html
<html>
<head>
<script type="text/javascript">
</script>
</head>
<body>
<span>文本框值為:</span><span id="span1"></span><br />
<input type="button" value="獲取父窗口內的文本框值" onclick="getpValue();">
</body>
</html>
下面來舉幾個常用的例子:
parent.window與top.window一般在分割的頁面即 frameset或iframe中使用
注銷整個框架后返回到login.aspx:parent.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();
就總結到這里,這些對象很實用