DIV CSS 佈局教程網

Popup.js 彈出框進階改造:修好 Firefox/Chrome 遮罩失效,還能回調伺服器端方法
編輯:關於JavaScript     

之前分享過一版 Popup.js,功能本身沒什麼大毛病,但這個版本的重點在於「更實用」:除了把跨瀏覽器的遮罩層缺陷補掉,還加上「按下確認後回調伺服器端方法」的能力。對於 ASP.NET 開發,或是習慣用 Ajax 做互動的專案來說,這個改進價值相當高。

原版 Popup.js 的兼容性陷阱

當頁面頂端帶有 XHTML 的 DOCTYPE 宣告時,問題就浮現了:在 IE6/7/8 底下遮罩層可以正常鋪滿整個視窗,但換到 Firefox 與 Chrome 就只蓋住一小塊。

根本原因出在負責產生遮罩層的那段程式碼:

var C = '<div id="dialogBoxBG" style="position:absolute;top:0px;left:0px;width:100%;height:100%;z-index:' + A + ";" + H + "background-color:" + this.color.cColor + ';display:none;"></div>';

dialogBoxBG 的高度只寫了 height:100%,在標準模式下瀏覽器會把它換算成「相對於 containing block」的高度,而不是視窗高度,於是 FF 與 Chrome 就露餡了。

那個「完美版」為什麼還是不完美

網路上流傳的 luocheng 版本號稱完美,實際下載測試後,遮罩層依然沒能鋪滿 Firefox 與 Chrome。讀過原始碼後找到兩個問題:

其一,新增的 getValue 方法裡,switch 的 case "clientHeight": 竟然寫了兩次,刪掉重複的那筆之後仍然沒解決。

其二,setBackgroundSize 把算出來的高度直接指派給 style.height,這是個純數值,不符合 Web 標準:

setBackgroundSize: function() {
  var getValueWidth;
  var getMaxValueWidth = [getValue("clientWidth"), getValue("scrollWidth")];
  getValueWidth = eval("Math.max(" + getMaxValueWidth.toString() + ")");
  G('dialogBoxBG').style.width = getValueWidth;
  var getValueHeight;
  var getMaxValueHeight = [getValue("clientHeight"), getValue("scrollHeight")];
  getValueHeight = eval("Math.max(" + getMaxValueHeight.toString() + ")");
  G('dialogBoxBG').style.height = getValueHeight;
},

修法只有一行:

G('dialogBoxBG').style.height = getValueHeight + "px";

所以往後寫程式的時候,寬高這類數值最好一律補上 'px' 單位,這是最容易忽略、也最容易釀成跨瀏覽器災情的地方。

附錄:各瀏覽器取頁面高度的差異

clientHeight:IE 與 FF 的行為大致相同,指的是瀏覽器可視區域的高度,也就是扣掉工具列、狀態列之後實際能呈現內容的空間。

scrollHeight:IE 下代表頁面實際內容高度,數值可以小於 clientHeight;FF 下代表網頁內容高度,但最小值會被 clamp 在 clientHeight。

擴充用法一:確認框回調伺服器端方法

function ShowConfirm(title, content, target) //顯示確認對話框
{
  var pop = new Popup({
    contentType: 3,
    isReloadOnClose: false,
    width: 350,
    height: 110
  });
  pop.setContent("title", title);
  pop.setContent("confirmCon", content);
  pop.setContent("callBack", ShowCallBackServer); //回調函數
  pop.setContent("parameter", {
    id: "divCall",
    str: target,
    obj: pop
  });
  pop.build();
  pop.show();
  popp = pop;
  return false;
}

//執行服務器端方法,即進行 __doPostBack('','') 操作
function ShowCallBackServer(para) {
  var str = para["str"];
  if ("" != str && null != str) {
    str = GetEachBtnName(str);
    if ("" != str && null != str) {
      __doPostBack(str, '');
    }
  }
  ClosePop();
}

//遍歷頁面中的 Button 名稱
function GetEachBtnName(obj) {
  return obj.name == '' || obj.name == null ? obj.id : obj.name;
}

使用方式:在帶有 OnClick="btnTest_Click" 的 Button 上註冊 OnClientClick,寫成 return ShowConfirm(' ','是否確定刪除?',this) 即可。

<asp:Button ID="btnDel" runat="server" Text="刪除" OnClick="btnDel_Click" OnClientClick="return ShowConfirm(' ','是否確定刪除?',this)" />

擴充用法二:在 iframe 場景中彈出對話框

頁面內嵌 iframe 時,常見的困窘是對話框只能蓋住 iframe 自己的範圍。若希望它浮在父頁面上、遮罩真正全屏,並在確認後回頭觸發 iframe 內的伺服器端方法,可以這樣寫:

function ShowConfirmIFrame(title, content, target) //顯示確認對話框
{
  var pop = new Popup({
    contentType: 3,
    isReloadOnClose: false,
    width: 350,
    height: 110
  });
  pop.setContent("title", title);
  pop.setContent("confirmCon", content);
  pop.setContent("callBack", ShowIFrame); //回調函數
  pop.setContent("parameter", {
    id: "divCall",
    str: target,
    obj: pop
  });
  temp = target;
  pop.build();
  pop.show();
  popp = pop;
  return false;
}

var temp;
function ShowIFrame() {
  parent.frames["content"].window.ShowCallBackServerIFrame(temp);
}

function ShowCallBackServerIFrame(para) {
  var str = para;
  if ("" != str && null != str) {
    str = GetEachBtnName(str);
    if ("" != str && null != str) {
      __doPostBack(str, '');
    }
  }
  closeWin();
}

接著在 iframe 頁面裡定義一個轉呼用的函式:

//刪除
function subDel(obj)
{
  return parent.parentDel(obj);
}

Button 控制項註冊 OnClientClick:

<asp:Button ID="btnDel" runat="server" OnClick="btnDel_Click" ToolTip="刪除" CssClass="deleteBtn" OnClientClick="return subDel(this);return false;" />

父頁面則負責接收並真的把對話框跳出來:

function parentDel(obj)
{
  return ShowConfirmIFrame('刪除','是否確定刪除?',obj);
}

進化版與修正版都可下載,原版也一併修掉了前面提到的 FF/Chrome 遮罩問題。

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