JavaScript—window對象使用示例
1 打開新窗口
window.open(pageURL,name,parameters)
其中:
pageURL為子窗口路徑
name為子窗口句柄
parameters為窗口參數(shù)(各參數(shù)用逗號分隔)
如:
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)頁不是通過腳本程序打開的(window.open()),調(diào)用window.close()腳本關(guān)閉窗口前,必須先將window.opener對象置為null,否則瀏覽器(IE7、IE8)會彈出一個(gè)確定關(guān)閉的對話框。
<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()">
對于關(guān)閉框架窗口
<script language="javaScript">
function closeWindow()
{
window.opener = null;
window.open('', '_top', '');
window.parent.close();
}
</script>
4 location對象使用
window.location.reload();//刷新當(dāng)前頁
window.location.; //載入其他頁面
5 history對象使用
window.history.go(1); //前進(jìn)
window.history.go(-1); //后退
6 子窗體向父窗體傳值
6.1 簡單方法
(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();
另外,對于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>無標(biāo)題文檔</title>
<script type="text/javascript">
function opendialog()
{
window.showModalDialog("child.html",window,"win","resable=false");//這里用window對象作為參數(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>無標(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>
對于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>
請妥善保存你的賬戶信息,以免發(fā)生損失<BR>
帳號:<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í),我們也可以對子窗體的元素進(jìn)行賦值,因?yàn)閣indow.open函數(shù)同樣會返回一個(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í)還必須對父窗體的child變量進(jìn)行清空,否則打開子窗體后再關(guān)閉就無法再重新打開了:
<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>
相關(guān)文章
JavaScript 輸出顯示內(nèi)容(document.write、alert、innerHTML、console.log
剛開始接觸js的朋友肯定要接觸到j(luò)s的輸出,那么肯定會使用document.write、alert、innerHTML、console.log,這里簡單介紹一下,需要的朋友可以參考一下啊2016-12-12Javascript中的轉(zhuǎn)義用法實(shí)例代碼
Javascript 中的轉(zhuǎn)義用法一例,需要的朋友可以參考下。2010-11-11js中escape對應(yīng)的C#解碼函數(shù) UrlDecode
js中escape對應(yīng)的C#解碼函數(shù) System.Web.HttpUtility.UrlDecode(s),使用過程中有以下幾點(diǎn)需要注意2012-12-12Javascript學(xué)習(xí)筆記之 對象篇(一) : 對象的使用和屬性
Javascript 中的一切都可以視為對象,除了兩個(gè)特例:null 和 undefined。2014-06-06全面了解JavaScript的數(shù)據(jù)類型轉(zhuǎn)換
下面小編就為大家?guī)硪黄媪私釰avaScript的數(shù)據(jù)類型轉(zhuǎn)換。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家看,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07JavaScript CSS修改學(xué)習(xí)第三章 修改樣式表
在這一章我打算通過直接修改頁面的樣式表而不是通過訪問元素的辦法來修改PRE的背景顏色。不幸的是,瀏覽器嚴(yán)重的不兼容性讓這個(gè)代碼基本上不能使用。2010-02-02關(guān)于js中alert彈出窗口文本換行問題簡單詳細(xì)說明
js中alert彈出窗口文本換行是一個(gè)小問題,本人很是郁悶,于是搜集整理下,曬出來和大家分享2012-12-12JavaScript函數(shù)學(xué)習(xí)總結(jié)以及相關(guān)的編程習(xí)慣指南
這篇文章主要介紹了JavaScript函數(shù)學(xué)習(xí)總結(jié)以及相關(guān)的編程習(xí)慣指南,整理包含到了匿名函數(shù)和三元運(yùn)算符等非常cool的知識點(diǎn),需要的朋友可以參考下2015-11-11