JavaScript—window對(duì)象使用示例
更新時(shí)間:2013年12月09日 17:00:50 作者:
window對(duì)象是JavaScript瀏覽器對(duì)象模型中的頂層對(duì)象,其包含多個(gè)常用方法和屬性,下面為大家介紹下window對(duì)象的使用
window對(duì)象是JavaScript瀏覽器對(duì)象模型中的頂層對(duì)象,包含多個(gè)常用方法和屬性:
1 打開新窗口
window.open(pageURL,name,parameters)
其中:
pageURL為子窗口路徑
name為子窗口句柄
parameters為窗口參數(shù)(各參數(shù)用逗號(hào)分隔)
如:
window.open("http://www.cnblogs.com/zhouhb/","open",'height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
2 打開模式窗口
window.showModalDialog("http://www.cnblogs.com/zhouhb/","open","toolbars=0;width=200;height=200");
3 關(guān)閉窗口,不彈出提示框
如果網(wǎng)頁(yè)不是通過腳本程序打開的(window.open()),調(diào)用window.close()腳本關(guān)閉窗口前,必須先將window.opener對(duì)象置為null,否則瀏覽器(IE7、IE8)會(huì)彈出一個(gè)確定關(guān)閉的對(duì)話框。
<script language="javaScript">
function closeWindow()
{
window.opener = null;
window.open('', '_self', '');
window.close();
}
</script>
<input type='button' value='關(guān)閉窗口' onClick="closeWindow()">
或
<input type="button" value="關(guān)閉窗口" onClick="window.opener = null;
window.open('', '_self', '');window.close()">
對(duì)于關(guān)閉框架窗口
<script language="javaScript">
function closeWindow()
{
window.opener = null;
window.open('', '_top', '');
window.parent.close();
}
</script>
4 location對(duì)象使用
window.location.reload();//刷新當(dāng)前頁(yè)
window.location.; //載入其他頁(yè)面
5 history對(duì)象使用
window.history.go(1); //前進(jìn)
window.history.go(-1); //后退
6 子窗體向父窗體傳值
6.1 簡(jiǎn)單方法
(1)在父窗體中打開子窗體
var str=window.showModalDialog("s.html");
if(str!=null)
{
var v=document.getElementById("v");
v.value+=str;
}
(2)子窗體代碼
var v=document.getElementById("v");
window.parent.returnValue=v.value;
window.close();
另外,對(duì)于showModalDialog打開的窗口,也可以通過dialogArguments傳值:
父窗口代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無(wú)標(biāo)題文檔</title>
<script type="text/javascript">
function opendialog()
{
window.showModalDialog("child.html",window,"win","resable=false");//這里用window對(duì)象作為參數(shù)傳遞
}
</script>
</head>
<body>
<form>
<input type="text" id="name" />
<input type="button" id="open" value="open" onclick="opendialog()"/>
</form>
</body>
</html>
子窗口代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無(wú)標(biāo)題文檔</title>
<script type="text/javascript">
function updateParent()
{
var pwin=window.dialogArguments;//子窗口獲取傳遞的參數(shù)
if(pwin!=undefined)
{
pwin.document.getElementById("name").value=document.getElementById("name").value;
}
}
</script>
</head>
<body>
<form>
<input type="text" id="name" />
<input type="button" id="update" value="更新父窗口" onclick="updateParent()"/>
</form>
</body>
</html>
對(duì)于showModalDialog打開的窗口,也可以通過window.returnValue傳值:
主窗口:
<SCRIPT type="text/javascript">
function openWindow(){
var bankCard=window.showModalDialog("counter.html","","dialogWidth=400px;dialogHeight=200px");
alert("您的銀行卡密碼是"+bankCard+"\n");
}
</SCRIPT>
(2)打開的窗口
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>窗口練習(xí)</TITLE>
<SCRIPT type="text/javascript" language="javascript">
function closeWindow(){
window.returnValue=document.myform.cardPass.value;
window.close();
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="myform" action="" method="post">
賬戶信息:<BR>
請(qǐng)妥善保存你的賬戶信息,以免發(fā)生損失<BR>
帳號(hào):<INPUT name="cardNum" type="text" size="40"><BR>
密碼:<INPUT name="cardPass" type="password" size="45"><BR>
<INPUT type="button" name="btn" value="確認(rèn)" onClick="closeWindow()">
</FORM>
</BODY>
6.2 更加詳細(xì)的介紹
眾所周知window.open() 函數(shù)可以用來打開一個(gè)新窗口,那么如何在子窗體中向父窗體傳值呢,其實(shí)通過window.opener即可獲取父窗體的引用。
如我們新建窗體FatherPage.htm:
<script type="text/javascript">
function OpenChildWindow()
{
window.open('ChildPage.htm');
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />
然后在ChildPage.htm中即可通過window.opener來訪問父窗體中的元素:
<script type="text/javascript">
function SetValue()
{
window.opener.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
window.close();
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="SetFather" onclick="SetValue()" />
其實(shí)在打開子窗體的同時(shí),我們也可以對(duì)子窗體的元素進(jìn)行賦值,因?yàn)閣indow.open函數(shù)同樣會(huì)返回一個(gè)子窗體的引用,因此FatherPage.htm可以修改為:
<script type="text/javascript">
function OpenChildWindow()
{
var child = window.open('ChildPage.htm');
child.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />
通過判斷子窗體的引用是否為空,我們還可以控制使其只能打開一個(gè)子窗體:
<script type="text/javascript">
var child
function OpenChildWindow()
{
if(!child)
child = window.open('ChildPage.htm');
child.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />
光這樣還不夠,當(dāng)關(guān)閉子窗體時(shí)還必須對(duì)父窗體的child變量進(jìn)行清空,否則打開子窗體后再關(guān)閉就無(wú)法再重新打開了:
<body onunload="Unload()">
<script type="text/javascript">
function SetValue()
{
window.opener.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
window.close();
}
function Unload()
{
window.opener.child=null;
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="SetFather" onclick="SetValue()" />
</body>
1 打開新窗口
復(fù)制代碼 代碼如下:
window.open(pageURL,name,parameters)
其中:
pageURL為子窗口路徑
name為子窗口句柄
parameters為窗口參數(shù)(各參數(shù)用逗號(hào)分隔)
如:
復(fù)制代碼 代碼如下:
window.open("http://www.cnblogs.com/zhouhb/","open",'height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
2 打開模式窗口
復(fù)制代碼 代碼如下:
window.showModalDialog("http://www.cnblogs.com/zhouhb/","open","toolbars=0;width=200;height=200");
3 關(guān)閉窗口,不彈出提示框
如果網(wǎng)頁(yè)不是通過腳本程序打開的(window.open()),調(diào)用window.close()腳本關(guān)閉窗口前,必須先將window.opener對(duì)象置為null,否則瀏覽器(IE7、IE8)會(huì)彈出一個(gè)確定關(guān)閉的對(duì)話框。
復(fù)制代碼 代碼如下:
<script language="javaScript">
function closeWindow()
{
window.opener = null;
window.open('', '_self', '');
window.close();
}
</script>
<input type='button' value='關(guān)閉窗口' onClick="closeWindow()">
或
<input type="button" value="關(guān)閉窗口" onClick="window.opener = null;
window.open('', '_self', '');window.close()">
對(duì)于關(guān)閉框架窗口
復(fù)制代碼 代碼如下:
<script language="javaScript">
function closeWindow()
{
window.opener = null;
window.open('', '_top', '');
window.parent.close();
}
</script>
4 location對(duì)象使用
復(fù)制代碼 代碼如下:
window.location.reload();//刷新當(dāng)前頁(yè)
window.location.; //載入其他頁(yè)面
5 history對(duì)象使用
復(fù)制代碼 代碼如下:
window.history.go(1); //前進(jìn)
window.history.go(-1); //后退
6 子窗體向父窗體傳值
6.1 簡(jiǎn)單方法
(1)在父窗體中打開子窗體
復(fù)制代碼 代碼如下:
var str=window.showModalDialog("s.html");
if(str!=null)
{
var v=document.getElementById("v");
v.value+=str;
}
(2)子窗體代碼
復(fù)制代碼 代碼如下:
var v=document.getElementById("v");
window.parent.returnValue=v.value;
window.close();
另外,對(duì)于showModalDialog打開的窗口,也可以通過dialogArguments傳值:
父窗口代碼:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無(wú)標(biāo)題文檔</title>
<script type="text/javascript">
function opendialog()
{
window.showModalDialog("child.html",window,"win","resable=false");//這里用window對(duì)象作為參數(shù)傳遞
}
</script>
</head>
<body>
<form>
<input type="text" id="name" />
<input type="button" id="open" value="open" onclick="opendialog()"/>
</form>
</body>
</html>
子窗口代碼:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無(wú)標(biāo)題文檔</title>
<script type="text/javascript">
function updateParent()
{
var pwin=window.dialogArguments;//子窗口獲取傳遞的參數(shù)
if(pwin!=undefined)
{
pwin.document.getElementById("name").value=document.getElementById("name").value;
}
}
</script>
</head>
<body>
<form>
<input type="text" id="name" />
<input type="button" id="update" value="更新父窗口" onclick="updateParent()"/>
</form>
</body>
</html>
對(duì)于showModalDialog打開的窗口,也可以通過window.returnValue傳值:
主窗口:
復(fù)制代碼 代碼如下:
<SCRIPT type="text/javascript">
function openWindow(){
var bankCard=window.showModalDialog("counter.html","","dialogWidth=400px;dialogHeight=200px");
alert("您的銀行卡密碼是"+bankCard+"\n");
}
</SCRIPT>
(2)打開的窗口
復(fù)制代碼 代碼如下:
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>窗口練習(xí)</TITLE>
<SCRIPT type="text/javascript" language="javascript">
function closeWindow(){
window.returnValue=document.myform.cardPass.value;
window.close();
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="myform" action="" method="post">
賬戶信息:<BR>
請(qǐng)妥善保存你的賬戶信息,以免發(fā)生損失<BR>
帳號(hào):<INPUT name="cardNum" type="text" size="40"><BR>
密碼:<INPUT name="cardPass" type="password" size="45"><BR>
<INPUT type="button" name="btn" value="確認(rèn)" onClick="closeWindow()">
</FORM>
</BODY>
6.2 更加詳細(xì)的介紹
眾所周知window.open() 函數(shù)可以用來打開一個(gè)新窗口,那么如何在子窗體中向父窗體傳值呢,其實(shí)通過window.opener即可獲取父窗體的引用。
如我們新建窗體FatherPage.htm:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
function OpenChildWindow()
{
window.open('ChildPage.htm');
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />
然后在ChildPage.htm中即可通過window.opener來訪問父窗體中的元素:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
function SetValue()
{
window.opener.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
window.close();
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="SetFather" onclick="SetValue()" />
其實(shí)在打開子窗體的同時(shí),我們也可以對(duì)子窗體的元素進(jìn)行賦值,因?yàn)閣indow.open函數(shù)同樣會(huì)返回一個(gè)子窗體的引用,因此FatherPage.htm可以修改為:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
function OpenChildWindow()
{
var child = window.open('ChildPage.htm');
child.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />
通過判斷子窗體的引用是否為空,我們還可以控制使其只能打開一個(gè)子窗體:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
var child
function OpenChildWindow()
{
if(!child)
child = window.open('ChildPage.htm');
child.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />
光這樣還不夠,當(dāng)關(guān)閉子窗體時(shí)還必須對(duì)父窗體的child變量進(jìn)行清空,否則打開子窗體后再關(guān)閉就無(wú)法再重新打開了:
復(fù)制代碼 代碼如下:
<body onunload="Unload()">
<script type="text/javascript">
function SetValue()
{
window.opener.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
window.close();
}
function Unload()
{
window.opener.child=null;
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="SetFather" onclick="SetValue()" />
</body>
您可能感興趣的文章:
- javascript window對(duì)象屬性整理
- JavaScript的document對(duì)象和window對(duì)象詳解
- DOM_window對(duì)象屬性之--clipboardData對(duì)象操作代碼
- javascript 基礎(chǔ)篇4 window對(duì)象,DOM
- javascript學(xué)習(xí)筆記(十四) window對(duì)象使用介紹
- JS window對(duì)象的top、parent、opener含義介紹
- Javascript window對(duì)象詳解
- 淺談重寫window對(duì)象的方法
- jQuery獲得document和window對(duì)象寬度和高度的方法
- JavaScript Window瀏覽器對(duì)象模型方法與屬性匯總
相關(guān)文章
javascript設(shè)計(jì)模式之對(duì)象工廠函數(shù)與構(gòu)造函數(shù)詳解
這篇文章主要介紹了javascript設(shè)計(jì)模式之對(duì)象工廠函數(shù)與構(gòu)造函數(shù)詳解,使用對(duì)象字面量,或者向空對(duì)象中動(dòng)態(tài)地添加新成員,是最簡(jiǎn)單易用的對(duì)象創(chuàng)建方法,除了這兩種常用的對(duì)象創(chuàng)建方式,JavaScript還提供了其他方法創(chuàng)建對(duì)象,需要的朋友可以參考下2015-07-07
Javascript 實(shí)現(xiàn)簡(jiǎn)單計(jì)算器實(shí)例代碼
這篇文章主要介紹了Javascript 實(shí)現(xiàn)簡(jiǎn)單計(jì)算器實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10
javascript如何創(chuàng)建表格(javascript繪制表格的二種方法)
利用js來動(dòng)態(tài)創(chuàng)建表格有兩種格式,appendChild()和insertRow、insertCell()。兩種方式其實(shí)差不多,但第一種有可能在IE上有問題,所以推薦大家使用第二種方法,看下面的解決和使用方法2013-12-12
javascript實(shí)現(xiàn)用戶必須勾選協(xié)議實(shí)例講解
這篇文章主要介紹了javascript實(shí)現(xiàn)用戶必須勾選協(xié)議實(shí)例講解,寫頁(yè)面的時(shí)候經(jīng)常會(huì)用到,有感興趣的同學(xué)可以學(xué)習(xí)下2021-03-03
Dojo Javascript 編程規(guī)范 規(guī)范自己的JavaScript書寫
良好的JavaScript書寫習(xí)慣的優(yōu)點(diǎn)不言而喻,今天彬Go向大家推薦Dojo Javascript 編程規(guī)范,相當(dāng)不錯(cuò)的 Javascript 編程風(fēng)格規(guī)范,建議大家可以借鑒一下此規(guī)范編寫 Javascript。感謝i.feelinglucky的翻譯2014-10-10

