JavaScript—window对象使用示例
发布时间:2024-05-08 09:39:34
window对象是JavaScript浏览器对象模型中的顶层对象,包含多个常用方法和属性:
1 打开新窗口
window.open(pageURL,name,parameters)
其中:
pageURL为子窗口路径
name为子窗口句柄
parameters为窗口参数(各参数用逗号分隔)
如:
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 关闭窗口,不弹出提示框
如果网页不是通过脚本程序打开的(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.href="http://www.cnblogs.com/zhouhb/"; //载入其他页面
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对象作为参数传递
}
</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;//子窗口获取传递的参数
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>
请妥善保存你的账户信息,以免发生损失<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:
<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函数同样会返回一个子窗体的引用,因此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>


猜你喜欢
- 本文实例讲述了Python深拷贝与浅拷贝用法。分享给大家供大家参考,具体如下:1、对象的赋值对象的赋值实际上是对象之间的引用:当创建一个对象
- 一、正则表达式的作用提示:正则表达式是一段特殊的字符串,它表示的是一段有规律的信息。如果我们想从一段文字中提取想要的内容,就可以通过正则表达
- 什么是协程协程(Coroutine)是一种比线程更加轻量级的并发方式,它不需要线程上下文切换的开销,可以在单线程中实现并发。协程通常具有以下
- 常用方法#记住引入numpy时要是用别名np,则所有的numpy字样都要替换 #查询数值类型>>>type(float)d
- 1. Mysql备份某个数据库的命令####################################################
- 一个方便的特性是你可以给一个视图指定默认的参数。 这样,当没有给这个参数赋值的时候将会使用默认的值。例子:# urls.pyfrom dja
- 在Python中定义一个数据便在内存中开辟一片空间来存储这个变量的值,这块已经被分配的内存空间便会有一个内存地址。访问这块内存需要用到变量名
- 在大型的ASP项目中,很多的页面都涉及到分页、翻页功能。如果每个页面都写一个翻页的程序的话,这样的工作即降低了工作效率,也不利于工程的模块化
- 1.首先在项目中添加必备js与css 2.代码中添加引用(必备引用) <script src="Scripts/
- 安装Pycharm进行Python开发时,经常右下角提示No R interpreter defined,处理方式:1、安装R,然后将R的路
- 本篇文章用到 element官网 和 七牛云官网element-ui 官网:https://element.eleme.io/#/zh-CN
- 前言一直都觉得vue的插件生涩难懂,但是又很好奇,在看了几篇文章,试着写了写之后觉得也没那么难,本文主要实现一个简单的Toast插件,方便迁
- 本文实例为大家分享了JavaScript/jQuery实现切换页面效果的具体代码,供大家参考,具体内容如下<!DOCTYPE html
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN&
- python连接clickhouse数据库在Python中获取系统信息的一个好办法是使用psutil这个第三方模块。顾名思义,psutil
- 我就废话不多说了,直接上代码吧!'''python对象销毁(垃圾回收)'''class Po
- 直接使用Navicat通过IP连接会报各种错误,例如:Error 1130: Host '192.168.1.80' is
- 看代码: HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transi
- 本文实例讲述了Python利用正则表达式匹配并截取指定子串及去重的方法。分享给大家供大家参考。具体如下:import repattern=r
- 1、检测登录状态base.pydef checkLogin(func):""" 查看session