当前页面: 开发资料首页 → Javascript 专题 → JavaScript写打开窗口
摘要: JavaScript写打开窗口
网页都是建立在窗口之上的,任何有关网页的事情,都与窗口有关。下面首先让我们熟悉一下大家常用的Internet Explorer的基本结构。
正常情况下,打开的窗口都有如下四部分:
在默认情况下,打开的新窗口都含有以上部分。但是当用到windows.open()时,我们就可以控制新打
开的窗口的风格了。
语法:
<table class=code cellSpacing=0 cellPadding=4 align=center bgColor=#cccccc border=0> <tr> <td vAlign=center>oNewDoc = document.open(sUrl [, sName] [, sFeatures] [, bReplace])</td></tr></table>说明:
<table class=code cellSpacing=2 cellPadding=4 width=565 border=0> <tr> <td width=87 bgColor=#999999> sURL</td> <td class=unnamed1 width=446 bgColor=#cccccc colSpan=2>可选。字符串--指定新的文档的URL地址。如果没有指定这一项,那么将会是一个空的(about:blank)。</td></tr> <tr> <td width=87 bgColor=#999999 rowSpan=7> sName</td> <td class=unnamed1 width=446 bgColor=#cccccc colSpan=2>可选。字符串--新生成窗口的名字。可以用作form或者a中Target的值。 </td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc> blank </td> <td width=291 bgColor=#ffffff>sURL被加载到一个新的未命名的窗口。</td></tr> <tr> <td width=149 bgColor=#cccccc> _media</td> <td width=291 bgColor=#eeeeee>sURL被加载到一个媒体栏中。(仅适合IE6以及以后版本浏览器)</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc> _parent</td> <td width=291 bgColor=#ffffff>sURL被加载到目前框架的上层框架上。如果没有上层框架,则此项的值与_self的值相同。</td></tr> <tr> <td width=149 bgColor=#cccccc> _search</td> <td width=291 bgColor=#eeeeee>sURL被打开在浏览器的查找窗口。(仅适合IE5以及以后的浏览器)</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc> _self</td> <td width=291 bgColor=#ffffff>sURL在当前的窗口上打开,覆盖以前的内容。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc> _top</td> <td width=291 bgColor=#eeeeee>sURL可能会加载到任何框架支架(Frameset)上,如果没有定义Frameset,此项值与_self的值相同。</td></tr> <tr> <td width=87 bgColor=#999999 rowSpan=15> sFeatures</td> <td class=unnamed1 width=446 bgColor=#cccccc colSpan=2>可选。字符串--列出对象表并用逗号分开。每一项都有自己的值,他们将被分开(如:"fullscreen=yes, toolbar=yes")。下面是被支持的各种特性。 </td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>channelmode = { yes | no | 1 | 0 }</td> <td class=unnamed1 width=285 bgColor=#ffffff>是否在窗口中显示阶梯模式。默认为no。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>directories = { yes | no | 1 | 0 }</td> <td class=unnamed1 width=285 bgColor=#eeeeee>是否在窗口中显示各种按钮。默认为yes。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>fullscreen = { yes | no | 1 | 0 }</td> <td class=unnamed1 width=285 bgColor=#ffffff>是否用全屏方式显示浏览器。默认为no。使用这一特性时需要非常小心。因为这一属性可能会隐藏浏览器的标题栏和菜单,你必须提供一个按钮或者其他提示来帮助使用者关闭这一浏览窗口。ALT+F4可以关闭窗口。一个全屏窗口必须使用阶梯(channelmode)模式。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>height = number</td> <td class=unnamed1 width=285 bgColor=#eeeeee>指定窗口的高度,单位是像素。最小值是100。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>left = number</td> <td class=unnamed1 width=285 bgColor=#ffffff>指定窗口距左边框的距离,单位是像素。值必须大于或者等于0。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>location = { yes | no | 1 | 0 }</td> <td class=unnamed1 width=285 bgColor=#eeeeee>指定是否在窗口中显示地址栏。默认为yes。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>menubar = { yes | no | 1 | 0 }</td> <td class=unnamed1 width=285 bgColor=#ffffff>指定是否在窗口中显示菜单栏。默认为yes。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>resizable = { yes | no | 1 | 0 }</td> <td class=unnamed1 width=285 bgColor=#eeeeee>指定是否在窗口中显示可供用户调整大小的句柄。默认为yes。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>scrollbars = { yes | no | 1 | 0 }</td> <td class=unnamed1 width=285 bgColor=#ffffff>指定是否在窗口中显示横向或者纵向滚动条。默认为yes。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>status = { yes | no | 1 | 0 }</td> <td class=unnamed1 width=285 bgColor=#eeeeee>指定是否在窗口中显示状态栏。默认为yes。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>titlebar = { yes | no | 1 | 0 }</td> <td class=unnamed1 width=285 bgColor=#ffffff>指定是否在窗口中显示标题栏。在非调用HTML Application或者一个对话框的情况下,这一项将被忽略。默认为yes。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>toolbar = { yes | no | 1 | 0 }</td> <td class=unnamed1 width=285 bgColor=#eeeeee>指定是否在窗口中显示工具栏,包括如前进、后退、停止等按钮。默认为yes。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>top = number</td> <td class=unnamed1 width=285 bgColor=#ffffff>指定窗口顶部的位置,单位是像素。值必须大于或者等于0。</td></tr> <tr> <td class=unnamed1 width=143 bgColor=#cccccc>width = number</td> <td class=unnamed1 width=285 bgColor=#eeeeee>指定窗口的宽度,单位是像素。最小值是100。</td></tr> <tr> <td width=87 bgColor=#999999 rowSpan=3> bReplace</td> <td width=452 bgColor=#cccccc colSpan=2>可选。当sURL被加载到同一窗口时,这个布尔型变量指定是否这个sURL新建立一个条目,或者是加到目前该窗口的历史记录上。 </td></tr> <tr> <td width=149 bgColor=#cccccc> true</td> <td width=291 bgColor=#ffffff>sURL覆盖当前文档的历史纪录。</td></tr> <tr> <td width=149 bgColor=#cccccc> false</td> <td width=291 bgColor=#eeeeee>sURL在历史记录中建立一个新的条目。</td></tr></table>
注释:
默认情况下,open方法建立一个拥有默认宽度、高度、菜单以及工具栏的窗口。你可以指定其中任何一项或者多项特征参数。这些参数是由一个或多个字符串所组成的。
当一旦有特征参数被指定,其他没有被指定的特征参数全部被禁止。因此,当用到特征参数,就需要定义所有在窗口中需要用到的其他特征参数。如果没有指定特征参数,那么所有的特征将会被保留原始的默认值。除了给特征参数指定新值之外,还可以简单的罗列特征名称来使得窗口的相应特征有效。
示例:
<table class=code cellSpacing=0 cellPadding=4 width="100%" align=center bgColor=#e0e0e0 border=0> <tr> <td colSpan=3>window.open("Sample.htm",null,
"height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
在例子中,建立了一个包含Sample.htm的新窗口。这个新窗口有200像素宽和400像素高,拥有状态栏,但是没有工具栏、菜单栏和地址栏。以上就是相应代码。
下面给出一个较为完整的例子,供大家参考新窗口的生成方法。
<table class=code cellSpacing=0 cellPadding=4 align=center bgColor=#e0e0e0 border=0> <tr> <td>
<head>Open Window
<script language="JavaScript">
function OpenWindow()
{
thisWin = window.open("", null, "width=300,height=400")
thisWin.document.write("<head>newWindow </head>")
thisWin.document.write("<body>This is a new window! </body>")
}
</script>
</head>
<body>
<input type="button" value="Creat New Window" onClick="OpenWindow()">
</body>
</td></tr></table>例子中的this.document.write是向新生成的窗口中写入代码,如果不这样,新生成的窗口将会显示错误。
生成新的窗口是比较简单的,大家只要熟练掌握上面介绍的特征,基本上就可以说掌握了新窗口的生成方法。