DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
彈出視窗生成器:用表單圖形化設定 toolbar、scrollbars 與視窗尺寸
編輯:CSS詳解     

每次要開新視窗,都得回頭查 window.open 的第三個參數怎麼拼,還容易漏掉逗號。這個小工具把這件事變成填表單:勾選要哪些功能、填數字設定寬高,按下去就自動產生可貼上的 JavaScript 程式碼。

核心的 generate() 函式

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function generate(form) {
  page       = document.inputForm.page.value;
  toolbars   = document.inputForm.toolbars.checked;
  scrollprops= document.inputForm.scrollit.checked;
  locations  = document.inputForm.locations.checked;
  statusbars = document.inputForm.statusbars.checked;
  menubars   = document.inputForm.menubars.checked;
  resizeable = document.inputForm.resizeable.checked;
  width      = document.inputForm.width.value;
  if (!width) width = screen.width;
  height     = document.inputForm.height.value;
  if (!height) height = screen.height;
  howto      = document.inputForm.howtoopen.options[document.inputForm.howtoopen.selectedIndex].value;
  center     = document.inputForm.center.checked;

  start = "<";

  // 依觸發方式產生第二段要貼的程式碼
  if (howto == 'body') {
    otherOutput = start + "!-- 步驟2: 修改 BODY 區域為以下代碼: -->\n\n" +
                  start + "BODY onLoad=\"javascript:popUp('" + page + "')\">";
  }
  if (howto == 'link') {
    otherOutput = start + "!-- 步驟2: 將下面的代碼貼到 BODY 區域內: -->\n\n" +
                  start + "A HREF=\"javascript:popUp('" + page + "')\">Open the Popup Window" + start + "/A>";
  }
  if (howto == 'button') {
    otherOutput = start + "!-- 步驟2: 將下面的代碼貼到 BODY 區域內: -->\n\n" +
                  start + "form>\n" +
                  start + "input type=button value=\"Open the Popup Window\" onClick=\"javascript:popUp('" + page + "')\">\n" +
                  start + "/form>";
  }

  // 組出步驟一的 popUp 函式
  scriptOutput = start + '!-- 使用下面代碼需要兩步: -->\r\n\r\n' +
                 '<!-- 步驟1: 拷貝下面代碼到 HEAD 區域內 -->\r\n\r\n' +
                 '<HEAD>\r\n\r\n' +
                 start + 'SCRIPT LANGUAGE="JavaScript">\r\n' +
                 'function pop' + 'Up(URL) {\nday = new Date();\nid = day.getTime();\n';

  scriptOutput += "eval(\"page\" + id + \" = window.open(URL, '\" + id + \"', '" +
    ((toolbars)    ? "toolbar=1,"    : "toolbar=0,") +
    ((scrollprops) ? "scrollbars=1," : "scrollbars=0,") +
    ((locations)   ? "location=1,"   : "location=0,") +
    ((statusbars)  ? "statusbar=1,"  : "statusbar=0,") +
    ((menubars)    ? "menubar=1,"    : "menubar=0,") +
    ((resizeable)  ? "resizable=1"   : "resizable=0") +
    ((width)  ? ",width="  + width  : "") +
    ((height) ? ",height=" + height : "") +
    ((center) ? ",left = " + ((screen.width  - width)  / 2) : "") +
    ((center) ? ",top = "  + ((screen.height - height) / 2) : "") +
    "');\");\n}\n// End -->\n" + start + "/sc" + "ript>\n";

  output = scriptOutput + "\n\n" + otherOutput;
  document.mail.source.value  = output;
  document.mail.source2.value = output;
  uncode();
}
// End -->
</SCRIPT>

幾個值得學的技巧

用三元運算子拼參數:((toolbars) ? "toolbar=1," : "toolbar=0,") 這種寫法把「勾選或不勾選」壓成一行,可讀性比長串 if 好得多。

視窗置中:(screen.width - width) / 2 就是最直覺的置中公式,扣掉視窗寬度再除以二,當成 left 與 top。

不重複的視窗名稱:用 new Date().getTime() 產生唯一 id 當視窗名,可以確保每次開啟都是新視窗而不是重用舊的。這個技巧至今仍常用。

不寫寬高就填滿螢幕:if (!width) width = screen.width 一行就把空值處理掉了,是很節省字數的預設值寫法。

今天的替代方案

彈出式視窗在現代瀏覽器已大幅受限,除了使用者點擊當下開啟之外幾乎都會被攔。同樣的需求現在改用 <dialog> 元素或搭配 CSS 的遮罩層(modal)實作,就能在頁面內完成,不受視窗管理限制,對無障礙與行動裝置也更友善。不過這段工具「把設定值即時組合成程式碼」的思路,做外掛設定頁或客製化產生器時依然好用。

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