当前位置:编程学习 > JS >>

js 打开窗口居中显示代码

文章收藏了二款打开窗口居中显示代码,他是利用了window.open来打开新窗口,但是居中利用了screen.availwidth/6*2来计算,然后再调整弹出窗口位置。

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.zhutiai.com/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>js 打开窗口居中显示代码</title>
<script language="网页特效">
//打开窗口居中显示
function openwindow(url,name,width,height)
{
 
 //alert(url);
 //宽默认400
 if(width=="" || width==0)
 {
  width=300;
 }
 //高默认300
 if(height=="" || height==0)
 {
  height=200;
 }
 //var winx=window.screen.width;
 //var winy=window.screen.height;
 var winx=width;
 var winy=height;
 var win=window.open(url,name,"width="+winx+",height="+winy+",toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,status=yes");
 //win.moveto(0,0);
 win.moveto((window.screen.width-width)/2,(window.screen.height-height)/2);
}

</script>
</head>

<body>
<a href="#"  onclick="javascript:openwindow('http://www.zzzyk.com','openurl',300,500)">打开窗口居中显示</a>
</body>
</html>

代码二

<script language="javascript">

function openwin(htmurl)

{

var url=htmurl; //要打开的窗口

var winname="newwin"; //给打开的窗口命名

var awidth=screen.availwidth/6*2; //窗口宽度,需要设置

var aheight=screen.availheight/5*2; //窗口高度,需要设置

var atop=(screen.availheight - aheight)/2; //窗口顶部位置,一般不需要改

var aleft=(screen.availwidth - awidth)/2; //窗口放中央,一般不需要改

var param0="scrollbars=0,status=0,menubar=0,resizable=2,location=0"; //新窗口的参数

//新窗口的左部位置,顶部位置,宽度,高度

var params="top=" + atop + ",left=" + aleft + ",width=" + awidth + ",height=" + aheight + "," + param0 ;


win=window.open(url,winname,params); //打开新窗口

win.focus(); //新窗口获得焦点

}

</script>
<p>javascript 打开一个居中显示的网页</p>

<p><input type="button" value="打开新窗口" name="b3" onclick="openwin('bb.html');"></p>

补充:网页制作,js教程 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,