DIV CSS 佈局教程網

JavaScript 彈出視窗技巧大全:開啟、關閉、跨視窗操作與參數詳解
編輯:關於JavaScript     

彈出視窗是早期 Web 開發中相當頻繁的操作,即便在今日的專案維護裡,仍常需要處理「子視窗關掉後刷新父視窗」這類問題。本文把實務上最常用到的幾組寫法整理成一份可直接查閱的清單。

一、關閉視窗的幾種情境

直接關閉,父視窗不跳提示:

 this.Response.Write("<script language=javascript>window.close();</script>");

父子視窗都不跳確認對話框:

 this.Response.Write("<script>");
this.Response.Write("{top.opener = null; top.close();}");
this.Response.Write("</script>");

這裡先把 top.opener 設為 null,是為了切斷子視窗對開啟者的參照,讓關閉動作不觸發瀏覽器的確認提示。

二、開啟視窗並控制外觀

帶尺寸、不要選單與工具列的視窗:

 this.Response.Write("<script language=javascript>window.open('rows.aspx','newwindow','width=200,height=200')</script>");

最基本的開啟方式,以及以 _blank 開新分頁:

 this.Response.Write("<script language=javascript>window.open('rows.aspx')</script>");
this.Response.Write("<script>window.open('WebForm2.aspx','_blank');</script>");

三、提示訊息與頁面跳轉的組合

先跳提示,確定後導向另一頁(仍在同一視窗):

 this.Response.Write(" <script language=javascript>alert('注冊成功');window.window.location.href='WebForm2.aspx';</script> ");

先跳提示,確定後再開新視窗:

 this.Response.Write("<script language='javascript'>alert('發表成功!');window.open('WebForm2.aspx')</script>");

先跳提示,確定後刷新父視窗:

 this.Response.Write("<script>alert('發表成功!');window.opener.location.href=window.opener.location.href;</script>");

四、父子視窗互相刷新

子視窗關閉的同時刷新父視窗,有兩種常見寫法:

 this.Response.Write("<script>window.opener.location.href=window.opener.location.href;window.close();</script>");
this.Response.Write("<script>window.opener.location.replace(window.opener.document.referrer);window.close();</script>");

子視窗只刷新父視窗而不關閉自己:

 this.Response.Write("<script>window.opener.location.href=window.opener.location.href;</script>");
this.Response.Write("<script>window.opener.location.href='WebForm1.aspx';</script>");

重新指定 location.href 為目前網址,是最直覺的「重新載入」手法;而 location.replace 不會在瀏覽紀錄中留下額外一筆,返回上一頁時體驗較好。

五、window.open 參數逐項說明

<SCRIPT LANGUAGE="javascript"> js 腳本開始;
window.open 彈出新視窗的命令;
'page.html' 彈出視窗的檔名;
'newwindow' 彈出視窗的名稱(不是檔名),非必填,可用空字串 '' 代替;
height=100 視窗高度;
width=400 視窗寬度;
top=0 視窗距螢幕上方的像素值;
left=0 視窗距螢幕左側的像素值;
toolbar=no 是否顯示工具列,yes 為顯示;
menubar、scrollbars 表示選單欄與捲軸;
resizable=no 是否允許改變視窗大小,yes 為允許;
location=no 是否顯示網址欄;
status=no 是否顯示狀態列資訊;
</SCRIPT> js 腳本結束

實際帶入參數的範例:

 window.open('detail.aspx?ID=' + e.Item.Cells[1].Text, 'newwin', 'width=750,height=600,scrollbars=yes,top=50,left=50');

this.Response.Write("<Script>window.open('WebForm2.aspx','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=750,height=470,left=80,top=40');</script>");

更完整的 IE 時代參數清單(各項以逗號分隔):

channelmode = { yes | no | 1 | 0 } 是否顯示階梯模式,預設 no。
directories = { yes | no | 1 | 0 } 是否顯示各種按鈕,預設 yes。
fullscreen = { yes | no | 1 | 0 } 是否以全螢幕顯示,預設 no。使用時需謹慎,因為可能隱藏標題欄與選單,務必提供關閉入口(ALT+F4 可關閉)。
height = number 視窗高度,單位像素,最小值 100。
left = number 視窗距左邊框的距離,單位像素,值需大於或等於 0。
location = { yes | no | 1 | 0 } 是否顯示網址欄,預設 yes。
menubar = { yes | no | 1 | 0 } 是否顯示選單欄,預設 yes。
resizable = { yes | no | 1 | 0 } 是否顯示可供使用者調整大小的控制點,預設 yes。
scrollbars = { yes | no | 1 | 0 } 是否顯示橫向或縱向捲軸,預設 yes。
status = { yes | no | 1 | 0 } 是否顯示狀態列,預設 yes。
titlebar = { yes | no | 1 | 0 } 是否顯示標題欄,非 HTML Application 或對話框情境下會被忽略。
toolbar = { yes | no | 1 | 0 } 是否顯示工具列(含上一頁、下一頁、停止等按鈕),預設 yes。
top = number 視窗頂部位置,單位像素,值需大於或等於 0。
width = number 視窗寬度,單位像素,最小值 100。

六、用函數統一管理彈窗

 <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 onload="openwin()">
任意的頁面內容...
</body>
</html>

定義好函數後,有四種常見的觸發方式:頁面載入時(body onload)、離開頁面時(body onunload)、點擊連結(<a href="#" onclick="openwin()">),以及點擊按鈕(<input type="button" onclick="openwin()" value="打開窗口">)。其中連結用的 # 屬於空連結,不會實際跳轉。

若要一次開兩個視窗,在同一函數中連續呼叫兩次即可,但務必用 top、left 錯開位置避免重疊,且兩個視窗的 name 不可相同:

 <script LANGUAGE="JavaScript">
<!--
function openwin() {
  window.open("page.html", "newwindow", "height=100, width=100, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no")
  window.open("page2.html", "newwindow2", "height=100, width=100, top=100, left=100, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no")
}
//-->
</script>

七、定時自動關閉與手動關閉按鈕

在彈出頁面的 <head> 加入計時器:

 <script language="JavaScript">
function closeit() {
  setTimeout("self.close()", 10000) // 單位為毫秒
}
</script>

再把該頁原本的 <BODY> 改為 <body onload="closeit()">,視窗就會在十秒後自行關閉。若想讓使用者自行決定關閉時機,放一顆按鈕更合適:

 <FORM>
<INPUT TYPE='BUTTON' VALUE='關閉' onClick='window.close()'>
</FORM>

八、不另存檔的純腳本彈窗

有時候我們不想為彈窗內容再開一個檔案,此時可以用 document.write 直接把 HTML 寫進新視窗:

 <html>
<head>
<SCRIPT LANGUAGE="JavaScript">
function openwin() {
  OpenWindow = window.open("", "newwin", "height=250, width=250, toolbar=no, scrollbars=" + scroll + ", 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" onclick="openwin()" value="打開窗口">
</body>
</html>

寫這種內容時要特別小心標籤成對,多一個或少一個都可能讓整段結構跑掉,最後別忘了呼叫 OpenWindow.document.close() 收尾。

九、用 Cookie 做到「只彈一次」

如果彈窗被放在首頁這類高頻造訪的頁面,每次整理都彈一次會非常惱人。用 cookie 記錄一個標記就能避免:

 <script>
function openwin(){
  window.open("page.html", "", "width=200,height=200")
}
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') == '') {
    openwin();
    document.cookie = "popped=yes";
  }
}
</script>

接著把主頁面的 <BODY> 換成 <body onload="loadpopup()">(注意是 loadpopup 而非 openwin)。這樣重新整理或再次進入頁面,視窗都不會再跳出來。

結語

需要坦白說明的是,現代瀏覽器基於安全與使用者體驗考量,已大幅限制 window.open 的能力:非使用者直接觸發的自動彈窗通常會被阻擋,工具列與網址列等介面參數也多半被忽略。因此上述技巧較適合用於理解原理與維護既有系統;若是新專案,建議改用頁內的模態對話框元件,兼顧相容性與可用性。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved