彈出視窗是早期網頁最常被用到的技巧之一,從公告、注意事項到小廣告都靠它。本文把各種形式與控制手法整理成一份可查的清單。先提醒一句:現代瀏覽器全面攔截非使用者觸發的彈出視窗,以下寫法在實際專案中多已改用 <dialog> 或自製的遮罩層。
<body onload="window.open('https://www.jb51.net','','fullscreen');">
<b>www.jb51.net</b>
</body>
<body onload="window.open('https://www.jb51.net','','channelmode');">
<body onload="window.open('https://www.wangye8.com','','width=400,height=300,directories');">
這幾種模式化參數屬於 IE 時代的遺產,其他瀏覽器不支援。
<SCRIPT LANGUAGE="javascript">
<!--
showModalDialog('https://www.jb51.net', '',
'dialogWidth:400px;dialogHeight:300px;dialogLeft:200px;dialogTop:150px;
center:yes;help:yes;resizable:yes;status:yes');
//-->
</SCRIPT>
改用 showModelessDialog() 則是非模式視窗。兩者的差別:模式視窗會蓋在父視窗之上,不關掉就無法操作父視窗,容易招來使用者反感,盡量少用。
可用參數:
dialogHeight / dialogWidth:對話框的高與寬dialogLeft / dialogTop:相對於桌面左上角的座標center:是否在桌面居中,預設 yeshelp:是否顯示說明圖示,預設 yesresizable:是否可調整大小,預設 nostatus:是否顯示狀態列。非模式預設 yes,模式預設 no<SCRIPT LANGUAGE="javascript">
<!--
window.open('page.html');
//-->
</SCRIPT>
放在 <SCRIPT> 標籤之間;<!-- 與 --> 的包裹是為了舊瀏覽器,不支援 JavaScript 時不會把程式碼當文字顯示出來。路徑可用絕對或相對,單雙引號都行但不要混用。這段程式碼放在 HTML 任何位置都可以,越前面執行越早——頁面很長又想早點跳出來時就往前放。
<SCRIPT LANGUAGE="javascript">
<!--
window.open('page.html','newwindow',
'height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,
resizable=no,location=no,status=no');
// 實際使用請寫成一行
//-->
</SCRIPT>
參數說明:
page.html:要開啟的檔案newwindow:視窗名稱(不是檔名),可用空字串代替height / width:視窗高與寬top / left:距螢幕上方與左側的像素值toolbar:工具列;menubar:選單列;scrollbars:捲軸resizable:是否可縮放location:是否顯示網址列status:是否顯示狀態列資訊<html>
<head>
<script LANGUAGE="javascript">
<!--
function openwin() {
window.open("page.html","newwindow",
"height=100,width=400,toolbar=no,menubar=no,scrollbars=no,
resizable=no,location=no,status=no");
// 實際使用請寫成一行
}
//-->
</script>
</head>
<body> ...頁面內容... </body>
</html>
觸發方式:
<body onload="openwin()"> <!-- 讀入頁面時 --> <body onUnload="openwin()"> <!-- 離開頁面時 --> <a href="#" onclick="openwin()">開啟視窗</a> <!-- 用連結(# 是空連結) --> <input type="button" value="開啟視窗" onclick="openwin()">
<script language="javascript">
<!--
function openwin() {
window.open("page.html","","width=200,height=200");
}
//-->
</script>
<a href="1.htm" onclick="openwin()">open</a>
注意這段要加在 被彈出的 page.html 裡,不是主頁面:
<script language="javascript">
function closeit() {
setTimeout("self.close()", 10000); // 毫秒
}
</script>
<body onload="closeit()">
再加一顆按鈕讓使用者自己關:
<form> <INPUT TYPE='BUTTON' value='關閉' onclick="self.close()"> </form>
<html>
<head>
<SCRIPT LANGUAGE="javascript">
function openwin() {
var OpenWindow = window.open("", "newwin",
"height=250,width=250,toolbar=no,scrollbars=no,menubar=no");
OpenWindow.document.write("<TITLE>例子</TITLE>");
OpenWindow.document.write("<BODY BGCOLOR=#FFFFFF>");
OpenWindow.document.write("<H1>Hello!</H1>");
OpenWindow.document.write("New window opened!");
OpenWindow.document.write("</BODY>");
OpenWindow.document.write("</HTML>");
OpenWindow.document.close(); // 別忘了收尾
}
</script>
</head>
<body>
<a href="#" onclick="openwin()">開啟視窗</a>
<input type="button" value="開啟視窗" onclick="openwin()">
</body>
</html>
寫在 document.write() 裡的就是標準 HTML,多一行少一行標籤都會出錯,結尾務必呼叫 close()。
如果這段腳本放在首頁這種高頻頁面,每次重新整理都彈一次會非常惱人。用 Cookie 記一個旗標即可:
<script>
function openpopup() {
window.open("hello.htm","","width=300,height=300");
}
function get_cookie(Name) {
var search = Name + "=";
var returnvalue = "";
if (document.cookie.length > 0) {
var offset = document.cookie.indexOf(search);
if (offset != -1) {
offset += search.length;
var end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue = unescape(document.cookie.substring(offset, end));
}
}
return returnvalue;
}
function loadpopup() {
if (get_cookie('popped') == '') {
openpopup();
document.cookie = "popped=yes";
}
}
</script>
<body onload="loadpopup()"> <!-- 進入時彈(且只彈一次) -->
<script language="javascript">
<!--
var popup = null;
popup = window.open('', 'popupnav',
'width=500,height=500,resizable=0,scrollbars=auto');
if (popup != null) {
if (popup.opener == null) { popup.opener = self; }
popup.location.href = '要打開的檔名';
}
//-->
</script>
<script LANGUAGE="javascript">
<!--
function leave() {
window.open('1.htm', '',
'toolbar=no,menubar=no,location=no,height=225,width=235');
}
//END-->
</script>
<body onUnload="leave()">
腳本中的大小寫前後要一致。window.open 的第三個參數必須寫成一行,換行會讓整串失效。更重要的是:現代瀏覽器只允許在使用者點擊等直接互動中開啟視窗,onload 或 onUnload 觸發的彈出幾乎都會被攔下來。若要達到同樣的「焦點提示」效果,改用頁面內的遮罩層加 role="dialog" 的區塊,並記得讓使用者能明確認出並關掉它。