2014-06-25 16 views
10

Tôi đang quản lý một trang web cũ bị thủng với cửa sổ bật lên. Họ khá khó chịu vì họ cứ bị lạc sau cửa sổ chính. Tôi từ từ chuyển chúng sang một 'lightbox' hiện đại nhưng nó là một quá trình chậm và tẻ nhạt bởi vì tất cả các cửa sổ bật lên này đều chứa biểu mẫu và xác thực được thực hiện phía máy chủ, có nghĩa là tôi cần có thể gửi biểu mẫu và sau đó tái xuất hiện nếu có lỗi mà không vi phạm toàn bộ trang.showModalDialog thay thế?

Tôi vừa phát hiện ra có window.showDialogBox hoạt động hoàn hảo trong Firefox (ngăn bạn nhấp vào trang chính cho đến khi bạn đóng hộp thoại), nhưng Chrome đã không dùng nữa và IE chỉ hỗ trợ một nửa.

Vì vậy, có bất kỳ điều gì tôi có thể thay thế window.open trong thời gian trung bình để cung cấp trải nghiệm người dùng tốt hơn mà không cần phải viết lại mọi biểu mẫu để gửi và nhận JSON qua XHR không?

Trả lời

5

Bạn có thể sử dụng showModalDialog polyfill của mình bằng phần tử <dialog> phương thức hoàn toàn mới, hoạt động trong Google Chrome mới nhất. A <dialog> chèn lấp cho các trình duyệt cũ là here.

+1

Oh..that của rất mới. Chrome 37 thậm chí còn chưa ra mắt. Nó hoạt động độc đáo trong Firefox và không thực sự tồi tệ hơn so với những gì tôi đã có trước đây trong Chrome 36. Sẽ phải cung cấp cho nó một whirl. Cảm ơn! – mpen

+0

giải pháp polyfill không hoạt động với url của trang web chéo mà ngăn trình duyệt tải chúng vào iframe hoặc hộp thoại. Chẳng hạn như chuyển hướng oAuth2 vv Một giải pháp chung ít nhất phải dựa vào 'window.open' và callbacks. –

1

Bạn có thể sử dụng mã khác nhau cho các trình duyệt khác nhau, như thế này:

if(navigator.userAgent.indexOf("MSIE") != -1){  //If the browser is IE 
    //Code for IE 
} 
else if(navigator.vendor == "Firefox"){   //If the browser is Firefox 
    //Code for Firefox 
} 
else if(navigator.vendor == "Google Inc."){  //If the browser is Chrome 
    //Code for Chrome 
} 

Đối với IE, showModalDialog công trình tốt và nó ngăn cản bạn từ cách nhấn vào trang chính cho đến khi bạn đóng hộp thoại.
Đối với Firefox, bạn có thể sử dụng, như bạn đã nói, showDialogBox.
Đối với Chrome, bạn có thể sử dụng điều mà niutech đề xuất.

Nếu không, nếu bạn sử dụng window.open, tất cả cửa sổ bật lên sẽ nằm trên thanh tác vụ, nếu chúng bị ẩn sau cửa sổ, chỉ cần nhấp vào chúng trên thanh tác vụ để hiển thị chúng.

1

đây là mã của tôi:

/** 
* May 2015: showModalDialog will not be supported any longer, so, if not available, we need to make a pure 
* window.open and a catch which callbacks. 
* 
* In contradiction to other solutions, this "emulator" is capable of loading 
* cross-origin urls such as oAuth2 redirect cascades, which can not be put in to iframes or dialogs due to 
* their CSSP settings! 
* 
* Flow control of the caller needs to take care whether a window is returned or false 
* 
* @constructor showModalDialogHandler(callback) - callback is called, if window is closed. 
* 
*/ 
var showModalDialogHandler = function(callback) 
{ 
    this.modalDialogEmulator = null; 
    this.callBack = callback; 
    this.returnImmediately = false; 
    this.modalDialog = false; 
    this.maxRuns = 180; 
    this.intervall = 750; 
    this.force = false;    // if set to true, emulate always. 
    this.chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1; 
    /** 
    * make the call, open a dialog, load etc. 
    * 
    * @param url - URL to load 
    * @param arg - args to pass to the window 
    * @param feature - featurelist as of window.open 
    * @return - erturns a window object (if modal dialogs are supported) or false otherwise 
    * 
    */ 
    this.callModalDialog = function(url, arg, feature) { 
     var self = this; 

     if (!this.force && window.showModalDialog) 
      this.modalDialog = window.showModalDialog(url, arg, feature); 
     else 
     { 
      this.modalDialog = this.modalDialogEmulator(url, arg, feature); 
      window.setTimeout(function() { 
       self.modalDialog.focus(); 
      }, 20); 

      /* 
      * Catch lose focus on main window. Modal dialog should at least 
      * stay in front of the opener. If the opener gets focus, 
      * window is either shuffled up or closed and reopend (chrome). 
      * 
      */ 
      jQuery(window).bind("focus", function() { 
       //console.log("opener focus"); 
       if (self.chrome) 
       { 
        // brute force: close and reopen, hope it will cope with that !!! 
        if(!self.modalDialog.closed) 
        { 
         self.modalDialog.close(); 
         self.modalDialog = self.modalDialogEmulator(url, arg, feature);  
        } 
       } 
       else 
       { 
        if(!self.modalDialog.closed) 
        { 
         window.setTimeout(function() { 
          self.modalDialog.blur(); 
          self.modalDialog.focus(); 
         }, 30); 
        } 
        else 
        { 
         jQuery(window).unbind("focus"); // remove that listener, cpu-sucker. 
        } 
       } 
      }); 
     } 

     if (this.returnImmediately) 
     { 
      var runs = this.maxRuns; 
      var loop = setInterval(function() { 
       if(self.modalDialog.closed) 
       { 
        //console.log("close catched, callback:"); 
        clearInterval(loop); 
        self.callBack(); 
       } 
       if (runs-- <= 0) 
        clearInterval(loop); // infinitystopper 
      }, this.intervall); 
      return false; 
     } 
     else 
      return this.modalDialog; 

    }; 

    /* 
    * showModalDialog is not longer supported, emulate with popup and 
    * a catcher with returnImmediately 
    */ 
    if (this.force || !window.showModalDialog) 
    { 
     var self = this; 
     this.modalDialogEmulator = function(url, arg, feature) { 
      // console.log("window.ShowModalDialog not supported"); 
      self.returnImmediately = true; 
      var opFeature = feature.split(";"); 
      var featuresArray = new Array() 
      if (document.all) 
      { 
       for (var i = 0; i < opFeature.length - 1; i++) 
       { 
        var f = opFeature[i].split("="); 
        featuresArray[f[0]] = f[1]; 
       } 
      } 
      else 
      { 
       for (var i = 0; i < opFeature.length - 1; i++) 
       { 
        var f = opFeature[i].split(":"); 
        featuresArray[f[0].toString().trim().toLowerCase()] = f[1].toString().trim(); 
       } 
      } 

      var h = "200px", w = "400px", l = "100px", t = "100px", r = "yes", c = "yes", s = "no"; 
      if (featuresArray["dialogheight"]) h = featuresArray["dialogheight"]; 
      if (featuresArray["dialogwidth"]) w = featuresArray["dialogwidth"]; 
      if (featuresArray["dialogleft"]) l = featuresArray["dialogleft"]; 
      if (featuresArray["dialogtop"]) t = featuresArray["dialogtop"]; 
      if (featuresArray["resizable"]) r = featuresArray["resizable"]; 
      if (featuresArray["center"]) c = featuresArray["center"]; 
      if (featuresArray["status"]) s = featuresArray["status"]; 
      var modelFeature = "height = " + h + ",width = " + w + ",left=" + l + ",top=" + t 
         + ",model=yes,alwaysRaised=yes" + ",resizable= " + r + ",center=" + c 
         + ",dependent=yes,dialog=yes,modal=yes,close=0" 
         + ",status=" + s; 

      var model = window.open(url, "modal", modelFeature, null); 
      return model; 
     }; 
    } 

} 

cần jQuery, ít nhất.

+0

Bạn có bản demo không? – mpen

+0

Không, đó là xây dựng trong một ứng dụng cũ. Hãy xem liệu tôi có thể tạo ra một fiddle –

+0

Đây là một [jsfiddle] (http://jsfiddle.net/dwzu7010/2/) làm ví dụ làm việc. –

-1

bạn có thể kiểm tra this link cho jQuery Modal, sử dụng mã này, bạn cần phải bao gồm jquery-uijavascriptcss file