最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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 打開新窗口
復(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)頁不是通過腳本程序打開的(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)前頁
window.location.; //載入其他頁面

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

window.history.go(1); //前進(jìn)
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();

另外,對(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>無標(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>無標(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)閉就無法再重新打開了:
復(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>

相關(guān)文章

  • javascript設(shè)計(jì)模式之對(duì)象工廠函數(shù)與構(gòu)造函數(shù)詳解

    javascript設(shè)計(jì)模式之對(duì)象工廠函數(shù)與構(gòu)造函數(shù)詳解

    這篇文章主要介紹了javascript設(shè)計(jì)模式之對(duì)象工廠函數(shù)與構(gòu)造函數(shù)詳解,使用對(duì)象字面量,或者向空對(duì)象中動(dòng)態(tài)地添加新成員,是最簡單易用的對(duì)象創(chuàng)建方法,除了這兩種常用的對(duì)象創(chuàng)建方式,JavaScript還提供了其他方法創(chuàng)建對(duì)象,需要的朋友可以參考下
    2015-07-07
  • Javascript 實(shí)現(xiàn)簡單計(jì)算器實(shí)例代碼

    Javascript 實(shí)現(xiàn)簡單計(jì)算器實(shí)例代碼

    這篇文章主要介紹了Javascript 實(shí)現(xiàn)簡單計(jì)算器實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • 深入理解JavaScript中的對(duì)象

    深入理解JavaScript中的對(duì)象

    這篇文章主要介紹了深入理解JavaScript中的對(duì)象,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-06-06
  • 前端面試知識(shí)點(diǎn)目錄一覽

    前端面試知識(shí)點(diǎn)目錄一覽

    本文是小編給大家搜集整理的有關(guān)前端面試知識(shí)點(diǎn),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2019-04-04
  • javascript如何創(chuàng)建表格(javascript繪制表格的二種方法)

    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í)例講解

    這篇文章主要介紹了javascript實(shí)現(xiàn)用戶必須勾選協(xié)議實(shí)例講解,寫頁面的時(shí)候經(jīng)常會(huì)用到,有感興趣的同學(xué)可以學(xué)習(xí)下
    2021-03-03
  • Dojo Javascript 編程規(guī)范 規(guī)范自己的JavaScript書寫

    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
  • JavaScript 基礎(chǔ)篇(一)

    JavaScript 基礎(chǔ)篇(一)

    JavaScript 基礎(chǔ)篇,想要學(xué)習(xí)js的朋友可以參考下
    2012-03-03
  • JavaScript內(nèi)核之基本概念

    JavaScript內(nèi)核之基本概念

    本文將聚焦于JavaScript中的基本概念,這些概念與傳統(tǒng)語言有比較大的不同,因此單獨(dú)列出一章來做專門描述,理解本章的概念對(duì)書中后續(xù)章節(jié)的概念,代碼的行為等會(huì)有很大的幫助,讀者不妨花比較大的時(shí)間在本章,即使你對(duì)JavaScript已經(jīng)比較熟悉,也建議通讀本文
    2011-10-10
  • js的toLowerCase方法用法實(shí)例

    js的toLowerCase方法用法實(shí)例

    這篇文章主要介紹了js的toLowerCase方法用法,實(shí)例分析了toLowerCase方法用于轉(zhuǎn)換小寫的使用技巧,需要的朋友可以參考下
    2015-01-01

最新評(píng)論

南投市| 惠东县| 宜章县| 加查县| 江西省| 文山县| 新竹县| 保定市| 交城县| 科技| 朔州市| 乌海市| 饶阳县| 呼图壁县| 京山县| 越西县| 宜昌市| 阿拉善右旗| 满城县| 潞西市| 安溪县| 临潭县| 襄樊市| 湟中县| 进贤县| 洛扎县| 平顺县| 桂林市| 五台县| 隆回县| 乃东县| 星座| 徐汇区| 汉川市| 筠连县| 运城市| 韶山市| 收藏| 苏州市| 荣成市| 门源|