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

JavaScript—window對象使用示例

 更新時間:2013年12月09日 17:00:50   作者:  
window對象是JavaScript瀏覽器對象模型中的頂層對象,其包含多個常用方法和屬性,下面為大家介紹下window對象的使用
window對象是JavaScript瀏覽器對象模型中的頂層對象,包含多個常用方法和屬性:

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 關閉窗口,不彈出提示框

如果網(wǎng)頁不是通過腳本程序打開的(window.open()),調用window.close()腳本關閉窗口前,必須先將window.opener對象置為null,否則瀏覽器(IE7、IE8)會彈出一個確定關閉的對話框。
復制代碼 代碼如下:

<script language="javaScript">
function closeWindow()
{
 window.opener = null;
 window.open('', '_self', '');
 window.close();
}
</script>
<input type='button' value='關閉窗口' onClick="closeWindow()">

<input type="button" value="關閉窗口" onClick="window.opener = null;
window.open('', '_self', '');window.close()">

對于關閉框架窗口
復制代碼 代碼如下:

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

4 location對象使用
復制代碼 代碼如下:

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

5 history對象使用
復制代碼 代碼如下:

window.history.go(1); //前進
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>無標題文檔</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>無標題文檔</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>窗口練習</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:
復制代碼 代碼如下:

<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()" />

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

通過判斷子窗體的引用是否為空,我們還可以控制使其只能打開一個子窗體:
復制代碼 代碼如下:

<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()" />

光這樣還不夠,當關閉子窗體時還必須對父窗體的child變量進行清空,否則打開子窗體后再關閉就無法再重新打開了:
復制代碼 代碼如下:

<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設計模式之對象工廠函數(shù)與構造函數(shù)詳解

    javascript設計模式之對象工廠函數(shù)與構造函數(shù)詳解

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

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

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

    深入理解JavaScript中的對象

    這篇文章主要介紹了深入理解JavaScript中的對象,是JS入門學習中的基礎知識,需要的朋友可以參考下
    2015-06-06
  • 前端面試知識點目錄一覽

    前端面試知識點目錄一覽

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

    javascript如何創(chuàng)建表格(javascript繪制表格的二種方法)

    利用js來動態(tài)創(chuàng)建表格有兩種格式,appendChild()和insertRow、insertCell()。兩種方式其實差不多,但第一種有可能在IE上有問題,所以推薦大家使用第二種方法,看下面的解決和使用方法
    2013-12-12
  • javascript實現(xiàn)用戶必須勾選協(xié)議實例講解

    javascript實現(xiàn)用戶必須勾選協(xié)議實例講解

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

    Dojo Javascript 編程規(guī)范 規(guī)范自己的JavaScript書寫

    良好的JavaScript書寫習慣的優(yōu)點不言而喻,今天彬Go向大家推薦Dojo Javascript 編程規(guī)范,相當不錯的 Javascript 編程風格規(guī)范,建議大家可以借鑒一下此規(guī)范編寫 Javascript。感謝i.feelinglucky的翻譯
    2014-10-10
  • JavaScript 基礎篇(一)

    JavaScript 基礎篇(一)

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

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

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

    js的toLowerCase方法用法實例

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

最新評論

大化| 加查县| 崇义县| 正蓝旗| 石嘴山市| 佛冈县| 馆陶县| 文成县| 米泉市| 炉霍县| 东方市| 马龙县| 琼中| 富裕县| 绥化市| 龙岩市| 商丘市| 罗定市| 榆中县| 弥勒县| 普宁市| 天长市| 盐城市| 普兰店市| 鹰潭市| 鄂托克前旗| 莒南县| 泸溪县| 桂阳县| 定陶县| 黄骅市| 谢通门县| 南川市| 应城市| 剑川县| 东阳市| 河间市| 登封市| 贺兰县| 汉川市| 洛川县|