window.open()弹出窗⼝参数说明及居中设置window.open()可以弹出⼀个新的窗⼝,并且通过参数控制窗⼝的各项属性。
最基本的弹出窗⼝代码
window.open('httP://codeo/');
window.open()各参数详解
⽰例代码:
window.open('httP://codeo/', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no')各参数详解
‘httP://codeo/’ :弹出窗⼝的地址;
‘newwindow’ :弹出窗⼝的名字(不是⽂件名),⾮必须,可⽤空”代替;
自动弹窗代码
height=100 :窗⼝⾼度;
width=400 :窗⼝宽度;
top=0 :窗⼝距离屏幕上⽅的象素值;
left=0 :窗⼝距离屏幕左侧的象素值;
toolbar=no :是否显⽰⼯具栏,yes为显⽰;
menubar,scrollbars :表⽰菜单栏和滚动栏。
resizable=no :是否允许改变窗⼝⼤⼩,yes为允许;
location=no :是否显⽰地址栏,yes为允许;
status=no :是否显⽰状态栏内的信息(通常是⽂件已经打开),yes为允许;
弹出窗⼝居中⽅法
function openWin(url,name,iWidth,iHeight) {
//获得窗⼝的垂直位置
var iTop = (window.screen.availHeight - 30 - iHeight) / 2;
/
/获得窗⼝的⽔平位置
var iLeft = (window.screen.availWidth - 10 - iWidth) / 2;
window.open(url, name, 'height=' + iHeight + ',innerHeight=' + iHeight + '
        ,width=' + iWidth + ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + '
        ,status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=0,titlebar=no');
}
如何关闭弹出窗⼝?
在新打开页⾯中执⾏代码:
window.opener = null; window.open('', '_self'); window.close();
新窗⼝中关闭代码如下:
<input type="button" onclick="window.opener = null; window.open('', '_self'); window.close();" value="关闭" />