国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript―window對象使用示例

2019-11-20 21:30:05
字體:
供稿:網(wǎng)友
window對象是JavaScript瀏覽器對象模型中的頂層對象,包含多個常用方法和屬性:

1 打開新窗口
復(fù)制代碼 代碼如下:

window.open(pageURL,name,parameters)

其中:

pageURL為子窗口路徑

name為子窗口句柄

parameters為窗口參數(shù)(各參數(shù)用逗號分隔)

如:
復(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)頁不是通過腳本程序打開的(window.open()),調(diào)用window.close()腳本關(guān)閉窗口前,必須先將window.opener對象置為null,否則瀏覽器(IE7、IE8)會彈出一個確定關(guān)閉的對話框。
復(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()">

對于關(guān)閉框架窗口
復(fù)制代碼 代碼如下:

<script language="javaScript">
function closeWindow()
{
window.opener = null;
window.open('', '_top', '');
window.parent.close();
}
</script>

4 location對象使用
復(fù)制代碼 代碼如下:

window.location.reload();//刷新當前頁
window.location.; //載入其他頁面

5 history對象使用
復(fù)制代碼 代碼如下:

window.history.go(1); //前進
window.history.go(-1); //后退

6 子窗體向父窗體傳值

6.1 簡單方法

(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();

另外,對于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>無標題文檔</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>

子窗口代碼:
復(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>無標題文檔</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傳值:

主窗口:
復(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>
請妥善保存你的賬戶信息,以免發(fā)生損失<BR>
帳號:<INPUT name="cardNum" type="text" size="40"><BR>
密碼:<INPUT name="cardPass" type="password" size="45"><BR>
<INPUT type="button" name="btn" value="確認" onClick="closeWindow()">
</FORM>
</BODY>

6.2 更加詳細的介紹

眾所周知window.open() 函數(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()" />

其實在打開子窗體的同時,我們也可以對子窗體的元素進行賦值,因為window.open函數(shù)同樣會返回一個子窗體的引用,因此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()" />

通過判斷子窗體的引用是否為空,我們還可以控制使其只能打開一個子窗體:
復(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()" />

光這樣還不夠,當關(guān)閉子窗體時還必須對父窗體的child變量進行清空,否則打開子窗體后再關(guān)閉就無法再重新打開了:
復(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>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 黑水县| 蒲江县| 西和县| 南江县| 芦溪县| 惠水县| 宜都市| 冷水江市| 于田县| 内江市| 抚州市| 丰顺县| 东丽区| 陵川县| 四川省| 临沧市| 陇南市| 双柏县| 镇康县| 吉安县| 开原市| 明溪县| 邯郸县| 当涂县| 新疆| 扬州市| 兴和县| 巍山| 璧山县| 长治市| 高州市| 靖州| 友谊县| 义马市| 桑日县| 梁河县| 邹城市| 汕头市| 虹口区| 海兴县| 普兰县|