源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JavaScript—window对象使用示例

  • 时间:2022-09-04 14:53 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JavaScript—window对象使用示例
window对象是JavaScript浏览器对象模型中的顶层对象,包含多个常用方法和属性: 1 打开新窗口
[url=http://www.cnblogs.com/zhouhb/]
5 history对象使用 6 子窗体向父窗体传值 6.1 简单方法 (1)在父窗体中打开子窗体
[u]复制代码[/u] 代码如下:
var str=window.showModalDialog("s.html"); if(str!=null) { var v=document.getElementById("v"); v.value+=str; }
(2)子窗体代码
[u]复制代码[/u] 代码如下:
var v=document.getElementById("v"); window.parent.returnValue=v.value; window.close();
另外,对于showModalDialog打开的窗口,也可以通过dialogArguments传值: 父窗口代码:
[u]复制代码[/u] 代码如下:
<!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对象作为参数传递 } </script> </head> <body> <form> <input type="text" id="name" /> <input type="button" id="open" value="open" onclick="opendialog()"/> </form> </body> </html>
子窗口代码:
[u]复制代码[/u] 代码如下:
<!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;//子窗口获取传递的参数 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传值: 主窗口:
[u]复制代码[/u] 代码如下:
<SCRIPT type="text/javascript"> function openWindow(){ var bankCard=window.showModalDialog("counter.html","","dialogWidth=400px;dialogHeight=200px"); alert("您的银行卡密码是"+bankCard+"\n"); } </SCRIPT>
(2)打开的窗口
[u]复制代码[/u] 代码如下:
<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> 请妥善保存你的账户信息,以免发生损失<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() 函数可以用来打开一个新窗口,那么如何在子窗体中向父窗体传值呢,其实通过window.opener即可获取父窗体的引用。 如我们新建窗体FatherPage.htm:
[u]复制代码[/u] 代码如下:
<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来访问父窗体中的元素:
[u]复制代码[/u] 代码如下:
<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函数同样会返回一个子窗体的引用,因此FatherPage.htm可以修改为:
[u]复制代码[/u] 代码如下:
<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()" />
通过判断子窗体的引用是否为空,我们还可以控制使其只能打开一个子窗体:
[u]复制代码[/u] 代码如下:
<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变量进行清空,否则打开子窗体后再关闭就无法再重新打开了:
[u]复制代码[/u] 代码如下:
<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>
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部