window.open()可以彈出一個新的窗口,並且通過參數控制窗口的各項屬性。
最基本的彈出窗口代碼
window.open('httP://codeo.cn/');
window.open()各參數詳解
示例代碼:
window.open('httP://codeo.cn/', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no')
各參數詳解
- ‘httP://codeo.cn/’ :彈出窗口的地址;
- ‘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="關閉" />