JavaScript compatible with all browsers Open dialog box solutions

Source: Internet
Author: User
Tags file upload

use JavaScript member access features to implement the universal version of the Compatible all browser Open dialog box features

Open web Page dialog box, there are generally three ways: window.open, window.showModalDialog, Window.showmodelessdialog, each has its advantages and disadvantages. The first method: Window.Open is a commonly used method, it is compatible with all browsers, open a new page window is very simple, but this method opens the "dialog" it by default is not support interaction (such as: Get the return value) and suspend operation (ie: after opening, if not closed, The other page windows are not operational and are in a pending state; the second and third method: window.showModalDialog, Window.showmodelessdialog Support Interactive operation and if the use of ShowModalDialog also support the modal dialog box, can implement other page window is suspended, meet the criteria of the dialog box, but because these two methods only support IE browser, so, unless the user is required to specify in IE Browse operation Web page, Otherwise, it is not recommended to use. dialog box in fact, in the actual Web site class system is essential, in order to solve the dialog box compatibility and interaction features, I for the JavaScript member access characteristics (all members are in the form of key-value pairs, You can access it through a member accessor, such as: Window.alert, or you can access it through a key, such as "alert" window[, to implement an interactive common dialog box.

First, to define the page contents of the dialog box, the code is as follows:

windialog.html

<! doctype html public  "-//w3c//dtd xhtml 1.0 transitional//en"   "http://
Www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <html xmlns=" http://www.w3.org/1999/xhtml "> <head>     <title></title> </head> <body>      <table id= "optionstable" >         <thead>              <tr>                  <th>   No. </th>                  <th> Surname   Name </th >                 <th> Sex   </th>                  <th>Learning   Calendar </th>             </tr>          </thead>         < Tbody>             <tr>                  <td>0001</td>                  <td> John </td >                 <td> Men </td>                 
<td> Undergraduate </td>             </tr>             <tr>        &nbsP;         <td>0002</td>                  <td> Zhang Qi </td>   
              <td> Women </td>                 <td> Undergraduate </td>             </tr>              <tr>                  <td>0003</td>                  <td> Dick </td>   
              <td> male </td>      &Nbsp;          <td> Specialist </td>              </tr>              <tr>                  <td>0004</td>                  <td> Zhao Liu </td>                  <td> Men </td>                  <td> Doctor </td>              </tr>          </tbody>     </table> <script type= "Text/javascript" >      var rEquest = getrequest ();
    var cancel = true;       window.onload = function  ()  {         var tbody = document.getelementbyid ("Optionstable"). GetElementsByTagName ("
Tbody "). Item (0);         for  (var i = 0; i <  tbody.rows.length; i++)  {            var
 row = tbody.rows[i];             row.onmouseover = function   ()  {               
 this.style.backgroundColor =  "Red";             }              row.onmouseout = function  ()  {           
     this.style.backgroundColor =  "";             }              row.onclick = function  ()  {    
            if  (request["complete"])  {                      window.opener[request["complete"]] (getselectvalues (this))  //method to callback the parent window                      window.cancel
 = false;                   
  window.close ();                 }              }              &NBSP}       window.onunload = function  ()  {         if  (cancel && request["Cancel"])  {             window.opener[request["Cancel"]] ();  // Methods to callback the parent window              }          //Get URL Parameters     function getrequest ()  {         var url = location.search; //gets the "?" in the URL.
Character string         var therequest = new object ();         if  (Url.indexof ("?")  != -1)  {            var str
 = URL.SUBSTR (1);
            strs = str.split ("&");             for  (var i = 0;  i < strs.length; i++)  {                 therequest[strs[i].split ("=") [0]] =  (strs[i].split ("=") [1]
);             }       
  }         return theRequest;    &NBSP}       //gets an array of the contents of the cells of the currently selected row     function  getselectvalues (Row)  {        var values = [];         for  (var i = 0; i <  row.cells.length; i++)  {            
values[i] = row.cells[i].innerhtml;         }         return 
Values    &NBSP} </script> </body> </html>



The key in the preceding code is the method statement that recalls the parent window: window.opener[request["complete"]] (getselectvalues (this)); window.opener[ request["Cancel"]] (), or it can be invoked dynamically using the Eval form.

Two, and then look at how the parent window (that is, the calling window) pops up the dialog box and interacts with the code as follows:

winopner.html

<! Doctype html> <html> <head>     <title> parent window </title> </head > <body>     <p>         <input  id= "Text1"  type= "text"  /><input id= "Button1"  type= "button"  value= "Open child window"  />     </p>     <script type= "Text/javascript" >         window.onload = function  ()  {             document.getelementbyid ("Button1"). onclick  = function  ()  {                 var windialog = window.open ("Windialog.html?complete=dialog_complete &cancel=dialog_cancel the callback method specified in the//parameter                          "_blank", "Toolbar=no,menubar=no,scrollbars=auto, Resizable=no,location=no,status=no,depended=yes,width=600,height=500,left= " +  (screen.width -
 600)  / 2 +  ", top="  +  (screen.height - 500)  / 2);
            };         }            Function dialog_complete (Result)  { //callback method when completed         
    alert (result);         }            Function dialog_cancel () callback method when  {//dialog box closes           
  alert ("cancel!");        &NBSP}     </script> </body> </html &Gt 



In addition to using the Window.Open method to open a new window in the preceding code, it is particularly necessary to note that the URL contains the callback method parameters that are agreed to in the Windialog.html dialog page: Complete=dialog_complete&cancel =dialog_cancel, this will automatically callback the Dialog_complete method when the dialog box is opened and the Dialog_cancel method will be recalled if the shutdown is canceled.

Let's take a look at the effect screenshot:

To open a dialog box:

Select a row (click) data, callback the Dialog_complete method in the parent window, eject the selected data, and of course you can write the data to the text box:

If you do not choose to close directly (that is, cancel), callback the Dialog_cancel in the parent window:

See these, there is no feel good, I think still can, of course, some people may say the above URL should include callback method parameters, if the parameter name or method name is wrong, it will cause callback unsuccessful, this is a problem, There are also every call page to repeat a bunch of window.open parameters is not good, can be encapsulated into an object? Of course, I thought of all this, and I realized it, see the following package code:

Webdialog.js defined as Webdialog object:

var webdialog = function  (url, width, height, resizeable)  { 
   this.left =  (screen.width - width)  / 2;
    this.top =  (screen.height - height)  / 2;
    this.complete = function  (Result)  { };
    this.cancel = function  ()  { };
    var _self = this;     this.show = function  ()  {         var _features =  "toolbar=no,menubar=no,scrollbars=auto,resizable="  +  resizeable +  ", location=no,status=no,depended=yes,width="  + width +  ", height="
 + height +  ", left="  + _self.left +  ", top="  + _self.top;         var wiNdialog = window.open (url,  "Webdialogwindow",  _features);
        winDialog.dialog_complete = _self.complete;
        winDialog.dialog_cancel = _self.cancel;    &NBSP}}



Assigns the Window object returned by window.open to the Windialog variable in the preceding code, and then assigns the Dialog_complete and Dialog_cancel methods

Windialog2.html Define what the dialog box displays:

<! doctype html public  "-//w3c//dtd xhtml 1.0 transitional//en"   "http://
Www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <html xmlns=" http://www.w3.org/1999/xhtml "> <head>     <title></title> </head> <body>      <table id= "optionstable" >         <thead>              <tr>                  <th>   No. </th>                  <th> Surname   Name </th >                 <th> Sex   </th>                  <th>Learning   Calendar </th>             </tr>          </thead>         < Tbody>             <tr>                  <td>0001</td>                  <td> John </td >                 <td> Men </td>                 
<td> Undergraduate </td>             </tr>             <tr>        &nbsP;         <td>0002</td>                  <td> Zhang Qi </td>   
              <td> Women </td>                 <td> Undergraduate </td>             </tr>              <tr>                  <td>0003</td>                  <td> Dick </td>   
              <td> male </td>      &Nbsp;          <td> Specialist </td>              </tr>              <tr>                  <td>0004</td>                  <td> Zhao Liu </td>                  <td> Men </td>                  <td> Doctor </td>              </tr>          </tbody>     </table> <script type= "Text/javascript" >      var cancel = true;       window.onload = function  ()  {         var tbody = document.getelementbyid ("Optionstable"). GetElementsByTagName ("
Tbody "). Item (0);         for  (var i = 0; i <  tbody.rows.length; i++)  {            var
 row = tbody.rows[i];             row.onmouseover = function   ()  {               
 this.style.backgroundColor =  "Red";             }              row.onmouseout = function  ()  {                this.style.backgroundcolor =
  "";             }              row.onclick = function  ()  {                 var dialog_complete =  window["Dialog_complete"];//Get callback method                  if  (dialog_complete && typeof dialog_complete !=   "undefined")  {              
      dialog_complete (Getselectvalues (this));                   
  window.cancel = false;                    
 window.close ();                 }   
          }         }
   &NBSP}       window.onunload = function  ()  {         var dialog_cancel = window["Dialog_cancel"];  //Get callback method         if  (Cancel && dialog_ cancel && typeof dialog_cancel !=  "undefined")  {    
        dialog_cancel ();        &NBSP}     }       // Gets an array of contents for the currently selected row of cells     Function getselectvalues (Row)  {        var values 
= [];         for  (var i = 0; i <  row.cells.length; i++)  {            
values[i] = row.cells[i].innerhtml;         }         return 
Values    &NBSP}     </script> </body> </html>



In the preceding code, get the callback method by window["Dialog_complete"], window["Dialog_cancel", and then by judging whether there is a success, call it directly if it succeeds. The method called must be consistent with the signature of the specified callback method that was previously encapsulated. The

finally looks at how the same interaction can be done through the Webdialog object invocation, as follows:

winopner2.html

<! Doctype html> <html> <head>     <title> parent window </title>      <script type= "Text/javascript"  src= "Webdialog.js?v1" ></script> </ head> <body>     <p>         <input  id= "Text1"  type= "text"  /><input id= "Button1"  type= "button"  value= "Open child window"  />     </p>     <script type= "Text/javascript"
>         document.getelementbyid ("Button1"). Onclick=function () {             var dlg = new 
Webdialog ("windialog2.html", 600, 500,  "yes");             dlg.complete=function (Result) {// callback method when completed                  alert (result);
            };
            dlg.cancel=function () callback method when  {//cancel
                alert ("cancel!");
            };
            dlg.show ();
       &NBSP}       </script> </body> </html>



The end result is the same as the method before the encapsulation, and no screenshots are repeated here.

If the callback method is too cumbersome to write a callback method in every parent window, simply open the dialog box and assign the selected value to the specified text box, OK? Of course, add a Refillinputid property for Webdialog to the ID of the backfill text box, as follows:

Partial code:     this.refillinputid = null;//Backfill Frame ID     var _
self = this;     this.show = function  ()  {         var _features =  "toolbar=no,menubar=no,scrollbars=auto,resizable="  +  resizeable +  ", location=no,status=no,depended=yes,width="  + width +  ", height="
 + height +  ", left="  + _self.left +  ", top="  + _self.top;         var windialog = window.open (url,  "
Webdialogwindow ",  _features);
        winDialog.dialog_complete = _self.complete;
        winDialog.dialog_cancel = _self.cancel;         windialog.refillinputid = _self.refillinputiD     }



Then in the dialog content page, add the selection backfill value, as follows:

Row.onclick = function () {
var dialog_complete = window["Dialog_complete"];//Get callback method
if (dialog_complete    && typeof dialog_complete!= "undefined") {
dialog_complete (this));
var refillinputid = window["Refillinputid"]//Backfill text box
if (typeof Refillinputid = "string") {
Window.opener.document.getElementById (refillinputid). Value = Getselectvalues (this) [0];
}
Window.cancel = false;
Window.close ();



When this is invoked in the parent window, you can specify the callback method if you want to handle the result of the selection yourself, otherwise omit it and specify the backfill text box directly, as follows:

document.getElementById ("Button1") .onclick = function  ()  {    var
 dlg = new webdialog ("windialog2.html", 600, 500,  "yes");     //            dlg.complete =function (Result) {//Completion callback method     //         
       alert (result);
    //            };     //            dlg.cancel= function ()  {//callback method when canceled     //         
       alert ("cancel!");
    //            };     dlg.refillinputid =  "TEXT1";
    dlg.show (); }


The final effect is as follows:


Well, the dialog box is pretty much the same. I said that the method is not necessarily the best, but must be able to meet the basic requirements of the text in addition to the beginning of the interaction, there is a hang, this is not yet thought of methods, should be browser constraints, because if someone maliciously produced countless hanging windows, The computer will crash, of course, if there is a better way, welcome to exchange, thank you!



Compatible with all browser JavaScript Click Open Select File window

The File Upload button for chrome and IE is simple and difficult to read. I thought of a way to do it. <input type= "file" id= "file" style= "Display:none", and then use another <input type= "button" onclick= "File.Click ()"/>, this method seems to be nothing wonderful, the problem is under IE, FF can be very good operation, You just need to make the button look good on the display. Chrome does not have to eat this set, and finally have to let this input box display. Then rotate between the display and the hide. I seem to find that chrome is not so sensitive to my changes, I'm surprised to think that Chrome has cached , the thief is not likely.

I do not know why speculation is display:none reason, maybe Chrome think I do this, in deceiving the user, I had to surrender, changed to set style= "HEIGHT:0;WIDTH:0;DISPLAY:NONE9;", do not know why, It was a success, and then I tested it over and over in the developer tool, and as long as I checked "display:none", I couldn't get the file dialog to pop up. In IE and FF there is no need for such trouble.

&lt;! doctype&nbsp;html&nbsp;public&nbsp; "-//w3c//dtd&nbsp;xhtml&nbsp;1.0&nbsp;transitional//en" "http://www.w3.org/ Tr/xhtml1/dtd/xhtml1-transitional.dtd "&gt; &lt;html&gt; &lt;head&gt; &nbsp;&nbsp;&nbsp;&lt;title&gt; Compatible with all browser JavaScript click on open Select File window &lt;/title&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;script&nbsp;src= "http:// Ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js "&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input &nbsp;type= "File" &nbsp;id= "file" &nbsp;style= "height:0px;width:0px;" &gt; &lt;input&nbsp;type= "button" &nbsp;value= "click" &nbsp;id= "Brow" &gt; &lt;/body&gt; &lt;script&nbsp;language= " JavaScript "&gt; &lt;!--&nbsp;&nbsp;&nbsp;&nbsp;//input&nbsp;file&nbsp; style properties with a width high of zero are compatible with the Chrome browser, using display: None non-all browsers support &nbsp;&nbsp;&nbsp;&nbsp;$ (function&nbsp; () &nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ ("#brow"). Click (function&nbsp; () &nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;$ ("#file"). Click ();&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp; ($.browser.msie) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;showimg ();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ ("#file"). Change (function&nbsp; () &nbsp;{&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;showimg ();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;showimg () &nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;alert ($ ("#file"). Val ()); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp})//--&gt; &lt;/script&gt; &lt;/html &gt;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.