• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

JavaScript - Cross Browser Modal Dialog Box ...

The following JavaScript code snippet demonstrates how to create a wide variety of modal dialog boxes which force the user to answer the question prior to continuing on. To see this in action, JavaScript Modal Dialog Demo. The first block of JavaScript code can be put into a generic .js script file.

The second JavaScript block contains a couple of wrapper samples for creating a yes, no, cancel option as well as a yes, no, maybe option to demonstrate that you can create an unlimited number of customized dialogs.  You will probably want to create a few standard options and put them in your own generic .js script file.
It is pretty easy to force the window to be modal.  We will attack the issue in both the window.opener and the modal dialog window itself.  In the window.opener, we will use the window.setInterval() method to repeatedly check to see if our globally defined winmodal window is open.  If so, we will set its focus by using window.focus().  To avoid any extra window minimization of the modal dialog window, we will call window.focus() in the dialog window body tag is onblur() event.  When we launch the modal dialog window, we will also include the name of the method we want called when an option is selected by the user and the window is closed.  Notice how I opted to use eval() to dynamically fire the passed in JavaScript method when the window is closed to handle the business logic when the user selects a response.  Unlike typical modal dialogs in windows, you have to separate out the code the launches the window and the code that reacts to the response into two separate JavaScript functions.
I believe you will find this to be an effective way to create modal dialog windows.  In fact, it even handles the user clicking on non IE windows and then attempting to return to the window.opener or if the user tries to close the browser window by right clicking on it while in a minimized state.  I hope you found this little snippet helpful.


Modal Dialog Box Sample Code

<html>
<script language=JavaScript>
 
var ModalDialogWindow;
var ModalDialogInterval;
var ModalDialog = new Object;

ModalDialog.value = ;
ModalDialog.eventhandler = ;
 

function ModalDialogMaintainFocus()
{
  try
  {
    if (ModalDialogWindow.closed)
     {
        window.clearInterval(ModalDialogInterval);
        eval(ModalDialog.eventhandler);       
        return;
     }
    ModalDialogWindow.focus();
  }
  catch (everything) {   }
}
        
 function ModalDialogRemoveWatch()
 {
    ModalDialog.value = ;
    ModalDialog.eventhandler = ;
 }
        
 function ModalDialogShow(Title,BodyText,Buttons,EventHandler)
 {

   ModalDialogRemoveWatch();
   ModalDialog.eventhandler = EventHandler;

   var args=width=350,height=125,left=325,top=300,toolbar=0,;
       args+=location=0,status=0,menubar=0,scrollbars=1,resizable=0;  

   ModalDialogWindow=window.open("","",args);
   ModalDialogWindow.document.open();
   ModalDialogWindow.document.write(<html>);
   ModalDialogWindow.document.write(<head>);
   ModalDialogWindow.document.write(<title> + Title + </title>);
   ModalDialogWindow.document.write(<script + language=JavaScript>);
   ModalDialogWindow.document.write(function CloseForm(Response) );
   ModalDialogWindow.document.write({ );
   ModalDialogWindow.document.write( window.opener.ModalDialog.value = Response; );
   ModalDialogWindow.document.write( window.close(); );
   ModalDialogWindow.document.write(} );
   ModalDialogWindow.document.write(</script + >);        
   ModalDialogWindow.document.write(</head>);   
   ModalDialogWindow.document.write(<body onblur="window.focus();">);
   ModalDialogWindow.document.write(<table border=0 width="95%" align=center cellspacing=0 cellpadding=2>);
   ModalDialogWindow.document.write(<tr><td align=left> + BodyText + </td></tr>);
   ModalDialogWindow.document.write(<tr><td align=left><br></td></tr>);
   ModalDialogWindow.document.write(<tr><td align=center> + Buttons + </td></tr>);
   ModalDialogWindow.document.write(</body>);
   ModalDialogWindow.document.write(</html>);
   ModalDialogWindow.document.close();
   ModalDialogWindow.focus();
   ModalDialogInterval = window.setInterval("ModalDialogMaintainFocus()",5);

 }

</script>

<script language=JavaScript>


  function YesNoCancel(BodyText,EventHandler)
  {
     var Buttons=;
     Buttons = <a href=javascript:CloseForm("Yes");>Yes</a>  ;
     Buttons += <a href=javascript:CloseForm("No");>No</a>  ;
     Buttons += <a href=javascript:CloseForm("Cancel");>Cancel</a>  ;
     ModalDialogShow("Dialog",BodyText,Buttons,EventHandler);
  }

  function YesNoMaybe(BodyText,EventHandler)
  {
     var Buttons=;
     Buttons = <a href=javascript:CloseForm("Yes");>Yes</a>  ;
     Buttons += <a href=javascript:CloseForm("No");>No</a>  ;
     Buttons += <a href=javascript:CloseForm("Maybe");>Maybe</a>  ;
     ModalDialogShow("Dialog",BodyText,Buttons,EventHandler);
  }
 
 function YesNoCancelReturnMethod()
 {
   document.getElementById( amodalreturn1).value =  ModalDialog.value;
   ModalDialogRemoveWatch();
 }

 function YesNoMaybeReturnMethod()
{
    document.getElementById( amodalreturn2).value = ModalDialog.value;
    ModalDialogRemoveWatch();
}

</script>
 
<BODY >

  <table border=1 cellpadding=2 cellspacing=2 align=center width="60%">
    <tr><td align=left></td></tr>
    <tr><td align=left></td></tr>
    <tr><td align=left></td></tr>
    <tr>
       <td align=left><a href="javascript:YesNoCancel(Yes, no, or cancel me,
                                                                     YesNoCancelReturnMethod());">Show Modal #1</a>   
          1. <input type=text id=modalreturn1 name=modalreturn1 value=></td>
      </tr>
    <tr>
       <td align=left><a href="javascript:YesNoMaybe(Yes, no, or maybe me,
                              YesNoMaybeReturnMethod());">Show Modal #2</a>   
         2. <input type=text id=modalreturn2 name=modalreturn2 value=></td>
      </tr>
 
  </table>

</BODY>
</HTML>
   

source: eggheadcafe

 View Full Story.
Posted at 09:01:25 am | Permalink | Posted in Javascript  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

3 Comments |Add your comment.

Frank said:

The black background here is really annoying. And whoever posted this code is a moron. Did you actually test it? No way you did.

Posted at September 5, 2008 08:40:22 pm
Brian said:

Where are the Opening and Closing HEAD tags?

Posted at December 28, 2007 08:01:51 am
Brian said:

Where are the tags? It didnt work in IE7 as an HTML or HTA.

Posted at December 28, 2007 08:00:04 am

Your Comment ...

  Name (required)

  Email (required, hidden)

  Website


Top Stuff

e-messenger

MessengerFX

eBuddy

ILoveIM

AIM Express

Top 20 Ruby CMS


Our Partners

Facebook Applications

Ajax Projects

Web 2.0 Sites

Webloglines

Human Development Handbook

Software Development Company

Ajaxlines

Stock Exchange Chat


About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (111)
  • Articles (85)
  • Bookmarking (35)
  • Calendar (19)
  • Chat (39)
  • ColdFusion (3)
  • CSS (41)
  • Email (23)
  • Facebook (23)
  • Flash (15)
  • Games (6)
  • Google (28)
  • Html (14)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (36)
  • Javascript (171)
  • JSON (21)
  • Perl (2)
  • PHP (88)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (1)
  • Ruby (10)
  • Storage (4)
  • Toolkits (90)
  • Tutorials (199)
  • UI (12)
  • Utilities (167)
  • Web2.0 (13)
  • XmlHttpRequest (20)
  • YUI (4)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange