Dom编程(Window 对象)

Window对象 中包含的 对象:

对象 描述
clientInformation 包含关于 Web 浏览器的信息。
clipboardData 提供了对于预定义的剪贴板格式的访问,以便在编辑操作中使用。
document 代表给定浏览器窗口中的 HTML 文档。
event 代表事件状态,如事件发生的元素,键盘状态,鼠标位置和鼠标按钮状态。
external 允许访问由 Microsoft® Internet Explorer 浏览器组件宿主应用程序提供的附加对象模型。
history 包含了用户已浏览的 URL 的信息。
location 包含关于当前 URL 的信息。
navigator 包含关于 Web 浏览器的信息。
screen 包含关于客户屏幕和渲染能力的信息。


其中 navigator 对象 的 功能:导航器

属性 描述
appCodeName 获取浏览器的代码名称。
appMinorVersion 获取应用程序的次版本值。
appName 获取浏览器的名称。
appVersion 获取浏览器运行的平台和版本。
browserLanguage 获取浏览器的当前语言。
cookieEnabled 获取客户端的永久 cookie 是否在浏览器中启用。永久 cookies 是储存在客户端计算机上的。
cpuClass 获取指示 CPU 等级的字符串。
onLine 获取表明系统是否处于全局脱机模式的值。
platform 获取用户的操作系统名称。
systemLanguage 获取操作系统适用的默认语言。
userAgent 获取等同于 HTTP 用户代理请求头的字符串。
userLanguage 获取操作系统的自然语言设置。
集合 描述
plugins 获取文档中所有 EMBED 对象的集合。
方法 描述
javaEnabled 返回 Java 是否已启用。
taintEnabled 返回是否允许数据污点。
对象 描述
userProfile 提供了允许脚本对用户配置信息请求读取访问并执行读取操作的方法。

appName 浏览器名称、 appVersion 浏览器版本、platform用户操作系统名称:

<!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>window对象演示</title>
<script type="text/javascript">
function method(){
	var name = navigator.appName;
	var version = navigator.appVersion;
	var os = navigator.platform;
	alert(name+"........"+version+"........."+os);
}
</script>
</head>

<body>
<input type="button" οnclick="method()" value="window对象演示">
</body>
</html>

结果:

Netscape........5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.77.4 (KHTML, like Gecko) Version/7.0.5 Safari/537.77.4.........MacIntel



其中 history 对象的成员表:历史记录

属性 描述
length 获取历史列表中的元素数目。
方法 描述
back 从历史列表中装入前一个 URL。
forward 从历史列表中装入下一个 URL。
go 从历史列表中装入 URL。


其中 location 对象的成员表:当前的URL信息

属性 描述
hash 设置或获取 href 属性中在井号“#”后面的分段。
host 设置或获取 location 或 URL 的 hostname 和 port 号码。
hostname 设置或获取 location 或 URL 的主机名称部分。
href 设置或获取整个 URL 为字符串。
pathname 设置或获取对象指定的文件名或路径。
port 设置或获取与 URL 关联的端口号码。
protocol 设置或获取 URL 的协议部分。
search 设置或获取 href 属性中跟在问号后面的部分。
方法 描述
assign 装入新的 HTML 文档。
reload 重新装入当前页面。
replace 装入指定 URL 的另外文档来替换当前文档。

href 获取 或 设置 整个URL字符串:

<!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>window对象演示</title>
<script type="text/javascript">
function method(){
	//var url = window.location.href;
	//alert(url);
	
	window.location.href = "http://www.baidu.com";
}
</script>
</head>

<body>
<input type="button" οnclick="method()" value="window对象演示">
</body>
</html>


screen 对象:关于 客户屏幕 和 渲染能力 的信息。

属性 描述
availHeight 获取系统屏幕的工作区域高度,排除 Microsoft® Windows® 任务栏。
availWidth 获取系统屏幕的工作区域宽度,排除 Windows 任务栏。
bufferDepth 设置或获取用于画面外位图缓冲颜色的每像素位数。
colorDepth 获取用于目标设置或缓冲区的颜色每像素位数。
deviceXDPI 设置或获取系统屏幕水平每英寸点数(DPI)的数值。
deviceYDPI 设置或获取系统屏幕垂直每英寸点数(DPI)的数值。
fontSmoothingEnabled 获取用户是否在控制面板的显示设置中启用了圆整屏幕字体边角的选项。
height 获取屏幕的垂直分辨率。
logicalXDPI 获取系统屏幕水平每英寸点数(DPI)的常规数值。
logicalYDPI 获取系统屏幕垂直每英寸点数(DPI)的常规数值。
updateInterval 设置或获取屏幕的更新间隔。
width 获取屏幕的垂直分辨率。


avaiHeight 排除任务栏,屏幕的高度

avaiWidth 排除任务栏,屏幕的宽度

height 屏幕的高度

width 屏幕的宽度

 


 

其中 event 对象:代表事件的状态

属性 描述
AbstractMicrosoft® Internet Explorer 6 新增 使用 event 对象获取高级流重定向器(ASX)文件中项目横幅的 Abstract 内容。
altKey 设置或获取 Alt 键的状态。
altLeft 设置或获取左 Alt 键的状态。
BannerMicrosoft® Internet Explorer 6 新增 使用 event 对象获取高级流重定向器(ASX)文件中项目的 Banner 内容。
button 设置或获取用户所按的鼠标按钮。
cancelBubble 设置或获取当前事件是否要在事件句柄中向上冒泡。
clientX 设置或获取鼠标指针位置相对于窗口客户区域的 x 坐标,其中客户区域不包括窗口自身的控件和滚动条。
clientY 设置或获取鼠标指针位置相对于窗口客户区域的 y 坐标,其中客户区域不包括窗口自身的控件和滚动条。
contentOverflow 获取表明文档处理当前 LayoutRect 对象后是否包含附加的内容。
ctrlKey 设置或获取 Ctrl 键的状态。
ctrlLeft 设置或获取左 Ctrl 键的状态。
dataFld 设置或获取 oncellchange 事件影响的数据列。
fromElement 设置或获取事件发生时激活或鼠标将要离开的对象。
keyCode 设置或获取与导致事件的按键关联的 Unicode 按键代码。
MoreInfoMicrosoft® Internet Explorer 6 新增 通过 event 对象获取高级流重定向器(ASX)文件中项目横幅的 MoreInfo 内容。
nextPage 获取打印模板中下页的位置。
offsetX 设置或获取鼠标指针位置相对于触发事件的对象的 x 坐标。
offsetY 设置或获取鼠标指针位置相对于触发事件的对象的 y 坐标。
propertyName 设置或获取对象上发生更改的属性名称。
qualifier 设置或获取由数据源对象提供的数据成员的名称。
reason 设置或获取数据源对象的数据传输结果。
recordset 从数据源对象设置或获取对默认数据集的引用。
repeat 获取 onkeydown 事件是否正在重复。
returnValue 设置或获取事件的返回值。
saveType 当 oncontentsave 触发时获取剪贴板类型。
screenX 设置或获取获取鼠标指针位置相对于用户屏幕的 x 坐标。
screenY 设置或获取鼠标指针位置相对于用户屏幕的 y 坐标。
shiftKey 设置或获取 Shift 键的状态。
shiftLeft 设置或获取左 Shift 键的状态。
srcElement 设置或获取触发事件的对象。
srcFilter 设置或获取触发 onfilterchange 事件的滤镜对象。
srcUrn 获取触发事件的行为的统一资源名称(URN)。
toElement 设置或获取用户要将鼠标指针移动指向的对象的引用。
type 从 event 对象中获取事件名称。
wheelDeltaMicrosoft® Internet Explorer 6 新增 设置或获取滚轮按钮滚动的距离和方向。
x 设置或获取鼠标指针位置相对于父文档的 x 像素坐标。
y 设置或获取鼠标指针位置相对于父文档的 y 像素坐标。
集合 描述
bookmarks 返回捆绑到当前事件所影响的行上的 ActiveX® 数据对象(ADO)书签的集合。
boundElements 返回页面上所有绑定到给定数据集的所有元素的集合。
对象 描述
dataTransfer 提供了对于预定义的剪贴板格式的访问,以便在拖曳操作中使用。

event对象的 keyCode属性:你按的那个键的 Unicode 编码。

<!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>window演示</title>
<script type="text/javascript">
function press(){
	alert(window.event.keyCode);
}
</script>
</head>

<body>
<input type="text" οnkeypress="press()">
</body>
</html>

你在文本框中输入什么字符,就弹出这个字符的Unicode编码。

 


event对象的 returnValue属性:设置或获取事件的返回值。

文本框中只允许输入数字,如:QQ号。如出现其他字符,弹窗提示并终止事件。

<!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>window演示</title>
<script type="text/javascript">

function press(){
	if(!(window.event.keyCode>=48 && window.event.keyCode<=57)){
		alert("不允许输入非数字!");
		window.event.returnValue = false;	
	}
}
</script>
</head>

<body>
<input type="text" οnkeypress="press()">
</body>
</html>


event对象的 srcElement属性:事件源对象。

<!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>window演示</title>
<script type="text/javascript">
function press(){
	var node = window.event.srcElement;
	alert(node.nodeName+"..."+node.type);
}
</script>
</head>

<body>
<input type="text" οnkeypress="press()">
</body>
</html>

你现在触发的是哪一个节点,用srcElement就可以获取到。



 



window 对象的方法:

方法 描述
alert 显示包含由应用程序自定义消息的对话框。
attachEvent 将指定函数绑定到事件,以便每当该事件在对象上触发时都调用该函数。
blur 使元素失去焦点并触发 onblur 事件。
clearInterval 使用 setInterval 方法取消先前开始的间隔事件。
clearTimeout 取消先前用 setTimeout 方法设置的超时事件。
close 关闭当前浏览器窗口或 HTML 应用程序(HTA)。
confirm 显示一个确认对话框,其中包含一个可选的消息和确定取消按钮。
createPopup 创建弹出窗口。
detachEvent 从事件中取消指定函数的绑定,这样当事件触发时函数就不会收到通知了。
execScript 以给定语言执行指定的脚本。
focus 使得元素得到焦点并执行由 onfocus 事件指定的代码。
moveBy 将窗口的位置移动指定 x 和 y 偏移值。
moveTo 将窗口左上角的屏幕位置移动到指定的 x 和 y 位置。
navigate 在当前窗口中装入指定 URL。
open 打开新窗口并装入给定 URL 的文档。
print 打印与窗口关联的文档。
prompt 显示一个提示对话框,其中带有一条消息和一个输入框。
resizeBy 更改窗口的当前位置缩放指定的 x 和 y 偏移量。
resizeTo 将窗口的大小更改为指定的宽度和高度值。
scroll 将窗口滚动到自左上角起指定的 x 和 y 偏移量。
scrollBy 将窗口滚动 x 和 y 偏移量。
scrollTo 将窗口滚动到指定的 x 和 y 偏移量。
setActive 设置对象为当前对象而不将对象置为焦点。
setInterval 每经过指定毫秒值后计算一个表达式。
setTimeout 经过指定毫秒值后计算一个表达式。
showHelp 显示帮助文件。此方法可用于 Microsoft HTML 帮助。
showModalDialog 创建一个显示指定 HTML 文档的模式对话框。
showModelessDialog 创建一个显示指定 HTML 文档的非模式对话框。

alert 弹出一个对话框,只有一个确定按钮。

close 关闭当前窗口。

confirm 弹出一个对话框,包含两个按钮:确定和取消。

bConfirmed = window.confirm( [sMessage])

Return Value

Boolean.Returns one of the following possible values:

true The user clicked the OK button.
false The user clicked Cancel button.

  • confirm 里面可以有message。
  • 通常以 b 打头的 返回值 都是 Boolean型 的。

<!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>window演示</title>
<script type="text/javascript">
function method(){
	var b = confirm("what type is the dialog?");
	alert(b);
}
</script>
</head>

<body>
<input type="button" value="演示window对象" οnclick="method()">
</body>
</html>

我们用一个变量来记录 confirm 窗口的返回值。

当你点击 confirm 窗口的 确定 按钮,将会返回一个 true,利用 alert窗口 显示 返回值。

当你点击 confirm 窗口的 取消 按钮,将会返回一个 false,利用 alert窗口 显示 返回值。


moveBy 按照指定的偏移值进行偏移,点一下动一次。(只有IE支持)

window.moveBy(iX, iY)

<!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>window演示</title>
<script type="text/javascript">
function method(){
	window.moveBy(200,400);
}
</script>
</head>

<body>
<input type="button" value="演示window对象" οnclick="method()">
</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>window演示</title>
<script type="text/javascript">
function method(){
	for(var x=0; x<10; x++){
		window.moveBy(200,0);
		window.moveBy(0,100);
		window.moveBy(-200,0);
		window.moveBy(0,-100);
	}
}
</script>
</head>

<body>
<input type="button" value="演示window对象" οnclick="method()">
</body>
</html>


moveTo 移动到指定的位置。只动一次。

window.moveTo(iX, iY)
<!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>window演示</title>
<script type="text/javascript">
function method(){
	window.moveTo(300,100);
}
</script>
</head>

<body>
<input type="button" value="演示window对象" οnclick="method()">
</body>
</html>


resizeBy 将窗口变大指定的量。
resizeTo 将窗口变为指定的大小。

scroll 将窗口滚动到自左上角起 指定的 x 和 y 偏移量。
scrollBy 将窗口滚动 x 和 y 偏移量。
scrollTo 将窗口滚动到 指定的 x 和 y 偏移量。

prompt 可输入 对话框:

<!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>window演示</title>
<script type="text/javascript">
function method(){
	var text = window.prompt("我是可输入对话框,请输入内容","");
	alert("您输入的内容是:"+text);
}
</script>
</head>

<body>
<input type="button" value="演示window对象" οnclick="method()">
</body>
</html>


open 方法:

oNewWindow = window.open( [sURL] [, sName] [, sFeatures] [, bReplace])

Parameters

sURL Optional. String爐hat specifies the URL of the document to display. If no URL is specified, a new window with about:blank is displayed.
sName Optional. String爐hat specifies the name of the window. This name is used as the value for the TARGET attribute on a form or an aelement.
_blank The sURL is loaded into a new, unnamed window.
_media   The sURL is loaded into the HTML content area of the Media Bar. Available in Internet Explorer 6 or later.
_parent The sURL is loaded into the current frame's parent. If the frame has no parent, this value acts as the value _self.
_search Available in Internet Explorer 5 and later. The sURL is opened in the browser's search pane.
_self The current document is replaced with the specified sURL .
_top sURL replaces any framesets that may be loaded. If there are no framesets defined, this value acts as the value_self.
sFeatures Optional. This String parameter is a list of items separated by commas. Each item consists of an option and a value, separated by an equals sign (for example, "fullscreen=yes, toolbar=yes"). The following features are supported.
channelmode = { yes | no | 1 | 0 } Specifies whether to display the window in theater mode and show the channel band. The default is no.
directories = { yes | no | 1 | 0 } Specifies whether to add directory buttons. The default is yes.
fullscreen = { yes | no | 1 | 0 } Specifies whether to display the browser in full-screen mode. The default is no. Use full-screen mode carefully. Because this mode hides the browser's title bar and menus, you should always provide a button or other visual clue to help the user close the window. ALT+F4 closes the new window. A window in full-screen mode must also be in theater mode (channelmode).
height = number Specifies the height of the window, in pixels. The minimum value is 100.
left = number Specifies the left position, in pixels. This value is relative to the upper-left corner of the screen. The value must be greater than or equal to 0.
location = { yes | no | 1 | 0 } Specifies whether to display the input field for entering URLs directly into the browser. The default is yes.
menubar = { yes | no | 1 | 0 } Specifies whether to display the menu bar. The default is yes.
resizable = { yes | no | 1 | 0 } Specifies whether to display resize handles at the corners of the window. The default isyes.
scrollbars = { yes | no | 1 | 0 } Specifies whether to display horizontal and vertical scroll bars. The default is yes.
status = { yes | no | 1 | 0 } Specifies whether to add a status bar at the bottom of the window. The default is yes.
titlebar = { yes | no | 1 | 0 } Specifies whether to display a title bar for the window. This parameter is ignored unless the calling application is an HTML Application or a trusted dialog box. The default is yes.
toolbar = { yes | no | 1 | 0 } Specifies whether to display the browser toolbar, making buttons such as Back,Forward, and Stop available. The default is yes.
top = number Specifies the top position, in pixels. This value is relative to the upper-left corner of the screen. The value must be greater than or equal to 0.
width = number Sets the width of the window, in pixels. The minimum value is 100.
bReplace Optional. When the sURL is loaded into the same window, this Boolean parameter specifies whether the sURL creates a new entry or replaces the current entry in the window's history list.
true sURL replaces the current document in the history list
false sURL creates a new entry in the history list.

setTimeout 方法:

iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])

Parameters

vCode Required. Variant that specifies the function pointer or string that indicates the code to be executed when the specified interval has elapsed.
iMilliSeconds Required. Integer爐hat specifies the number of milliseconds.
sLanguage Optional. String爐hat specifies one of the following values:
JScript Language is JScript.
VBScript Language is VBScript.
JavaScript Language is JavaScript.

close 方法:

window.close()

点击按钮,广告窗口弹出,过5秒自动关闭:

<!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>window演示</title>
<script type="text/javascript">
function method(){
	window.open("ads.html", "_blank", "height=400, width=600, location=no, menubar=no, scrollbars=no, resizable=no, status=no, toolbar=no"); 
}
</script>
</head>

<body>
<input type="button" value="演示window对象" οnclick="method()">
</body>
</html>

ads.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>
</head>

<body>
<h1>特大喜讯</h1>
<h2>房子一折开卖啦!快来抢啊!</h2>
<h3>广告 5 秒之后,自动关闭。</h3>
<script type="text/javascript">
	window.setTimeout("window.close()", 5000);
</script>
</body>
</html>

setInterval 每隔指定时间,计算一次表达式:

每隔3秒,弹一次窗口:

<!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>window演示</title>
<script type="text/javascript">
function method(){
	window.setInterval("alert('interval')", 3000);
}
</script>
</head>

<body>
<input type="button" value="演示window对象" οnclick="method()">
</body>
</html>


clearTimeout 和 clearInterval:

clearInterval 使用 setInterval 方法取消先前开始的间隔事件。
clearTimeout 取消先前用 setTimeout 方法设置的超时事件。

清除刚才的计时弹窗:

<!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>window演示</title>
<script type="text/javascript">
function method(){
	var iIntervalID = window.setInterval("alert('interval')", 3000);
	clearInterval(iIntervalID);
}
</script>
</head>

<body>
<input type="button" value="演示window对象" οnclick="method()">
</body>
</html>


一打开页面,就往外蹦的广告:是窗体事件

事件 描述
onactivate 当对象设置为活动元素时触发。
onafterprint 对象所关联的文档打印或打印预览后立即在对象上触发。
onbeforedeactivate 在 activeElement 从当前对象变为父文档其它对象之前立即触发。
onbeforeprint 对象的关联文档打印或打印预览前在对象上触发。
onbeforeunload 在页面将要被卸载前触发。
onblur 在对象失去输入焦点时触发。
oncontrolselect 当用户将要对该对象制作一个控件选中区时触发。
ondeactivate 当 activeElement 从当前对象变为父文档其它对象时触发。
onerror 当对象装载过程中发生错误时触发。
onfocus 当对象获得焦点时触发。
onhelp 当用户在浏览器为当前窗口时按 F1 键时触发。
onload 在浏览器完成对象的装载后立即触发。
onmove 当对象移动时触发。
onmoveend 当对象停止移动时触发。
onmovestart 当对象开始移动时触发。
onresize 当对象的大小将要改变时触发。
onresizeend 当用户更改完控件选中区中对象的尺寸时触发。
onresizestart 当用户开始更改控件选中区中对象的尺寸时触发。
onscroll 当用户滚动对象的滚动条时触发。
onunload 在对象卸载前立即触发。

onload 当窗体加载完成后立即触发:

onbeforeunload  窗体关闭前触发

onunload 窗体关闭后触发

<!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>window演示</title>
<script type="text/javascript">
window.onload = function(){
	alert("onload");
}
window.onunload = function(){
	alert("onunload");
}
window.onbeforeunload = function(){
	alert("onbeforeunload");
}
</script>
</head>

<body>
<h1>我是一个页面!</h1>
</body>
</html>

onload 事件 所有浏览器都支持,onunload 和 onbeforeunload 只有IE支持。


做一个流氓广告弹窗,广告弹出关闭后又弹出:

<!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>window演示</title>
<script type="text/javascript">
window.onload = function(){
	window.open("ads.html", "_blank", "height=400, width=600, location=no, menubar=no, scrollbars=no, resizable=no, status=no, toolbar=no");
}
</script>
</head>

<body>
<h1>我是一个页面!</h1>
</body>
</html>

ads.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">
window.onunload = function(){
	window.open("ads.html", "_blank", "height=400, width=600, location=no, menubar=no, scrollbars=no, resizable=no, status=no, toolbar=no");
}
</script>
</head>

<body>
<h1>特大喜讯</h1>
<h2>房子一折开卖啦!快来抢啊!</h2>
</body>
</html>


focus() 方法:隔3秒 广告窗体会至于最上层。

<!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">

window.onload = function(){
	window.setInterval("window.focus()", 3000);
}

window.onunload = function(){
	window.open("ads.html", "_blank", "height=400, width=600, location=no, menubar=no, scrollbars=no, resizable=no, status=no, toolbar=no");
}

</script>
</head>

<body>
<h1>特大喜讯</h1>
<h2>房子一折开卖啦!快来抢啊!</h2>
</body>
</html>




window 对象的属性:

属性 描述
closed 获取引用窗口是否已关闭。
defaultStatus 设置或获取要在窗口底部的状态栏上显示的缺省信息。
dialogArguments 设置或获取传递给模式对话框窗口的变量或变量数组。
dialogHeight 设置或获取模式对话框的高度。
dialogLeft 设置或获取模式对话框的左坐标。
dialogTop 设置或获取模式对话框的顶坐标。
dialogWidth 设置或获取模式对话框的宽度。
frameElement 获取在父文档中生成 window 的 frame 或 iframe 对象。
length 设置或获取集合中对象的数目。
name 设置或获取表明窗口名称的值。
offscreenBuffering 设置或获取对象在对用户可见之前是否要先在屏幕外绘制。
opener 设置或获取创建当前窗口的窗口的引用。
parent 获取对象层次中的父窗口
returnValue 设置或获取从模式对话框返回的值。
screenLeft 获取浏览器客户区左上角相对于屏幕左上角的 x 坐标。
screenTop 获取浏览器客户区左上角相对于屏幕左上角的 y 坐标。
self 获取对当前窗口或框架的引用。
status 设置或获取位于窗口底部状态栏的信息。
top 获取最顶层的祖先窗口。

没讲。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值