Js打开网页后居中显示

使用JavaScript定义打开网页后居中显示,并可为窗口设置大小,使用“window.open”方法打开新窗口:先来看完整的代码及调用方法:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>打开居中的窗口</title>
<script language="javascript">
//参数-url:要打开的网站,winname:打开后的窗体名称
//参数width:打开新窗体的宽度,height:打开新窗体的高度
<!--
function openwindow( url,winName,width,height) 
{
    xposition=0; yposition=0;
    if ((parseInt(navigator.appVersion) >= 4 ))
    {
    xposition = (screen.width - width) / 2;//窗体居中的x坐标
    yposition = (screen.height - height) / 2;//窗体居中的y坐标
    }
    theproperty= "width=" + width + "," //打开窗口的属性
    + "height=" + height + "," 
    + "location=0," 
    + "menubar=0,"
    + "resizable=1,"
    + "scrollbars=0,"
    + "status=0," 
    + "titlebar=0,"
    + "toolbar=0,"
    + "hotkeys=0,"
    + "screenx=" + xposition + "," //仅适用于Netscape
    + "screeny=" + yposition + ","//仅适用于Netscape
    + "left=" + xposition + ","//IE
    + "top=" + yposition;//IE 
    window.open( url,winName,theproperty );//打开窗口
}//-->
</script>
</head>
<body>
<a href="javascript:openwindow('http://www.google.com','openwin',300,300)">打开窗口</a>
</body>
</html>

代码的重点是设置打开窗口的一些属性,此方法的三个参数分别代表:要打开窗口的URL、打开窗口的名字、打开窗口的一些属性(如是否有滚动条、最大化按钮 等)。本例使用“(screen.width-width)/2”和“(screen.height-height)/2”获取窗体居中的坐标点,然后指 定窗体的“left”和“top”属性。

posted @ 2015-03-12 22:36  壁虎漫步.  阅读(822)  评论(0编辑  收藏  举报