前文已经比较详细地谈过“pop window”的一些写法和技巧,现在再对一些没有涉及的内容做一些补充。
希望对大家有一些帮助。
(一)我们可以不用window.open的方法打开一个窗口,用下面的方法也可以作到:
代码如下:
<form action="example2.htm" target="anotherwindowname">
<input type="submit" value="open new window" name="submit">
</form>
(二)下面的例子首先用传统的方法打开一个小窗口,然后通过右面的连接将一个新的内容写入这个弹出的窗口中。
将文件调入弹出的窗口
代码如下:
<script language="javascript">
<!--
function openwin(){
window.open(′sample1.htm′,′mywindow′);
}
//-->
</script>
<a href="sample2.htm" target="mywindow">将文件写入弹出窗口</a>
(三)如果要显示一张图片,下面的方法也可以:
<form>
<input type="button" onclick="window.open(′picture.gif′,′′,′width=150,height=100′)" name="button" value="open picture">
</form>
(四)下面是一个弹出窗口的生成器:
url name width height directories location menubar scrollbars status toolbar resizeable
源代码如下:
<script language="javascript">
<!--
function createwindow(what) {
var url = what.url.value;
var windowname = what.windowname.value;
var features =
′width=′ + what.width.value +
′,height=′ + what.height.value +
′,directories=′ + (what.directories.checked - 0) +
′,location=′ + (what.location.checked - 0) +
′,menubar=′ + (what.menubar.checked - 0) +
′,scrollbars=′ + (what.scrollbars.checked - 0) +
′,status=′ + (what.status.checked - 0) +
′,toolbar=′ + (what.toolbar.checked - 0) +
′,resizeable=′ + (what.resizeable.checked - 0);
window.open (url, windowname, features);
}
//--></script>
<form>
url <input type="text" name="url" value="testpage.htm">
name <input type="text" name="windowname" value="mywindow">
width <input type="text" name="width" value="200">
height <input type="text" name="height" value="200">
<input type="checkbox" name="directories"> directories
<input type="checkbox" name="location"> location
<input type="checkbox" name="menubar"> menubar
<input type="checkbox" name="scrollbars"> scrollbars
<input type="checkbox" name="status"> status
<input type="checkbox" name="toolbar"> toolbar
<input type="checkbox" name="resizeable"> resizeable
<input type="button" value="create it"
onclick="createwindow(this.form)">
</form>
(五)弹出窗口是非常容易失去“焦点”的,即只要鼠标点击窗口以外的任意位置该弹出窗口的焦点就失去了。
用下面的方法可以对其进行控制,将如下代码加入弹出窗口中:
<body onblur="window.focus()">
但是,这样做有一个缺陷,即弹出的窗口永远被“聚焦”了,除非你强制关闭它。
没有关系,只要给它加一个“超时控制”即可,即让它只聚焦几秒钟,在这段事件内,访问者有足够的时间看清楚弹出窗口内的内容,然后只要用鼠标点窗口以外的任意位置,就自动关闭了。
<body onblur="settimeout=(′window.focus()′,1000)">
<body onblur="window.close()">
(六)定位你的窗口,使用screenx ,screeny(ns4) ,top ,left(ie4) 属性。
window.open(′testpage.htm′,′myexample6′,′width=200,height=200,screenx=400,screeny=400,top=400,left=400′);
(七)有没有想过让你的窗口永远居中?即不论窗口的大小时多少,一律显示在窗口的中间?用下面的脚本即可:
<script language="javascript">
<!--
function centerwindow() {
if (document.all)
var xmax = screen.width, ymax = screen.height;
else
if (document.layers)
var xmax = window.outerwidth, ymax = window.outerheight;
else
var xmax = 640, ymax=480;
var xoffset = (xmax - 200)/2, yoffset = (ymax - 200)/2;
window.open(′center.htm′,′myexample7′, ′width=200,height=200,screenx=′+xoffset+′,screeny=′+yoffset+′, top=′+yoffset+′,left=′+xoffset+′′);
}
centerwindow();
//-->
</script>
注意:本例的打开的窗口是200*200的,如果你的窗口不是这个尺寸,将上面的兰色部分改一下即可。
测试一下吧:
(八)下面的代码可以实现窗口内容的转跳,先打开一个窗口,经过3秒钟后页面自动跳转到另一个页面。
<script language="javascript">
<!--
mywindow8 = window.open(′blank.htm′,′myexample8′,′width=200,height=200′);
settimeout("mywindow8.location.href = ′testpage.htm′",3000);
//-->
</script>
(九)窗口内容的重写:
<script language="javascript">
<!--
msgwindow9 = window.open(′testpage.htm′,′myexample9′,′width=200,height=200′);
function update() {
for (var i=0; i < 10; i++)
msgwindow9.document.write(′message number ′ + i + ′<br>′);
}
settimeout(′update()′,3000); //3秒钟后写入。
//-->
</script>
说明:注意兰色部分相同,本例使用了一个循环写,作为演示比较清楚,你可以使用.document.write的方法将任何内容写入窗口中。
--------------------------------------------------------------------------------
闽公网安备 35060202000074号