Js之window对象使用

一.常用属性介绍:
1.name:Sets or retrieves the window or frame name.
例如:<iframe name="xxFrame"
2.location:Contains information about the current URL. 页面跳转
3.navigator:Contains information about the Web browser. 页面跳转
4.status:Sets or retrieves the message in the status bar at the bottom of the window.
状态栏内容,例如:
<SCRIPT LANGUAGE="JScript">
function reportMove() {
    window.status = "X=" + window.event.x + " Y=" + window.event.y;
}
</SCRIPT>
<BODY οnmοusemοve="reportMove()">
5.self:Retrieves a reference to the current window or frame.
例如:<a href="javascript:self.close()">
6.opener:Sets or retrieves a reference to the window that created the current window.
返回打开本窗口的窗口对象。主要针对调用window.open()方法的窗口;
可以使用window.opener.location.reload();实现主窗口(或父窗口)的刷新
可以使用window.opener.xxformname.submit()实现主窗口(或父窗口)的表单提交
如果向父窗口传递数据,可以使用:
window.opener.document.xxxparentformName.xxinputName.value= ' ';
window.close();
window.opener.focus();
常用刷新脚本:
(1)父窗口为普通窗口
window.opener.location="javascript:reloadPage();";
function reloadPage() {
         history.go(0);
         document.execCommand("refresh")
         document.location = document.location;
         document.location.reload();
}
(2)父窗口为普通frame窗口:
window.opener.parent.document.frames.item('xxFrame').location.href = window.opener.location.href;
注意:如果窗口不是由其他窗口打开的,在Netscape中返回 null;在IE中返回undefined
7.parent:Retrieves the parent of the window in the object hierarchy.
using:For a document, the parent is the containing window. For a window defined using FRAME,
the parent is the window that contains the corresponding FRAMESET definition
如果frame框架里的页面要改其他同框架下的页面或父框架的页面就用parent,例如:
window.parent.window.frames['xxFrame']
更新父窗体的数据:用window.parent.document.getElemnetById('xx').xxinputname.value=xx
在某个窗体对应的jsp,调用父窗体(即framesets)内元素:
window.parent.document.getElementByIdx_x_x_x("xxx").src;
8.top:tRetrieves the topmost ancestor window, which is its own parent.
返回占据整个浏览器窗口的最顶端的框架页对象,自己就是父对象
9.dialogArguments: 
Retrieves the variable or array of variables passed into the modal dialog window.
10.returnValue:
  Sets or retrieves the return value from the modal dialog window.

二.常用方法介绍:
1.open
对应方法: close() ,返回窗口对象
Opens a new window and loads the document specified by a given URL, or opens a blank document if a URL is not provided.
Returns a reference to the new window object. Use this reference to script properties and methods on the new window.
oNewWindow = window.open( [sURL] [, sName] [, sFeatures] [, bReplace])
例如:window.open("Sample.htm",null,"height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
子页面为父页面赋值,可使用如下脚本:
window.opener.document.formxxx.inputxxx.value= 'xxxx';
window.close();
window.opener.focus();

2.showModalDialog
模式对话框特点:关闭对话框后(子窗口)才能对父窗口进行操作,返回普通的属性值,如字符串
vReturnValue = window.showModalDialog(sURL [, vArguments] [, sFeatures])
Creates a modal dialog box that displays the specified HTML document.
Variant. Returns the value of the returnValue property as set by the window of the document specified in sURL
remarks:A modal dialog box retains the input focus while open. The user cannot switch windows until the dialog box is closed.
主从窗口数据传递方式:
(1)子窗口获取父窗口对象,并调用父窗口函数填充数据
父页面:需要传入当前window作为参数
showModelessDialog("callee.html",window,"status:false;dialogWidth:300px;dialogHeight:150px");
子页面:
var callerWindowObj = dialogArguments;
//对主窗口的任何对象进行操作
//callerWindowObj..document.forms[0].elements[0].value = "xxxx" ;
//window.dialogArguments.form1.index1.value="这是在设置index1元素的值"
callerWindowObj.jsGlobalValue =xxxxx;//父对象的input属性值(js变量值) = 子窗口获取值
//alert(window.dialogArguments.var_name)//读取xx变量
//window.dialogArguments.var_name="oyiboy"//设置xxe变量
callerWindowObj.callFun(); //调用父窗口的函数
(2)通过窗口的返回值实现
即通过子窗口的window.returnValue = xxxx实现
父页面:
ret = window.showModalDialog('xx.jsp','DialogWidth:450px;DialogHeight:350px;help:no;status:no');
alert(ret);
子页面:
<script>  
        window.returnValue = 'xxx';  
</script> 

3.showModelessDialog 
非模式对话框特点:鼠标可以对父,子窗口进行操作,返回窗口对象
vReturnValue = window.showModelessDialog(sURL [, vArguments] [, sFeatures])
Creates a modeless dialog box that displays the specified HTML document.
Variant. Returns a reference to the new window object. Use this reference to script properties and methods on the new window.
The modeless dialog box displays even when the user switches input focus to the window.
(1)子窗口获取父窗口对象,并调用父窗口函数填充数据
父页面:需要传入当前window作为参数
showModelessDialog("xx.html",window,"status:false;dialogWidth:300px;dialogHeight:150px");
子页面:
var callerWindowObj = dialogArguments;
//对主窗口的任何对象进行操作
//callerWindowObj..document.forms[0].elements[0].value = "xxxx" ;
//window.dialogArguments.form1.index1.value="这是在设置index1元素的值"
callerWindowObj.jsGlobalValue =xxxxx;//父对象的input属性值(js变量值) = 子窗口获取值
callerWindowObj.callFun(); //调用父窗口的函数
父页面简单例子:
var sColor=""; 
function callDialog() {
var t = showModelessDialog("xx.html",window,"status:false;dialogWidth:300px;dialogHeight:150px");
alert(t);//返回窗口对象
alert(sColor);//子对象操作父对象的变量sColor;即: dialogArguments.sColor = "xxxxx";
}


附录:
js访问表单元素:
document.forms[索引].elements[索引].属性/方法
document.表单名称.文本对象名称.方法属性/方法

 

Javascript刷新页面的几种方法:
history.go(0)
location.reload()
location=location
location.assign(location)
document.execCommand('Refresh')
window.navigate(location)
location.replace(location)
document.URL=location.href

 

window.open(URL,name,features,replace)参数说明:
URL
name 新窗口的名称;名称可以用作标记 <a> 和 <form> 的属性 target 的值;如果该参数指定了一个已经存在的窗口,
那么open() 方法就不再创建一个新窗口,而只是返回对指定窗口的引用
features 声明了新窗口要显示的标准浏览器的特征。如果省略该参数,新窗口将具有所有标准特征;
在窗口特征这个表格中,我们对该字符串的格式进行了详细的说明。
replace  一个可选的布尔值。规定了装载到窗口的 URL 是在窗口的浏览历史中创建一个新条目,还是替换浏览历史中的当前条目;
支持下面的值:true - URL 替换浏览历史中的当前条目;false - URL 在浏览历史中创建新的条目;


页面跳转方式:
(1) window.location.href="login.jsp?backurl="+window.location.href;
(2) window.history.back(-1);
(3) window.navigate("top.jsp");
(4) self.location='top.htm';
(5) top.location='xx.jsp';

 

本文转自:http://blog.sina.com.cn/s/blog_5ccfe6ad0100lrzv.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值