Click here to Skip to main content
15,881,715 members
Articles / Web Development / HTML

JavaScript Modal Popup Window

Rate me:
Please Sign up or sign in to vote.
4.84/5 (48 votes)
15 May 2014CPOL3 min read 695K   39.5K   55   136
Flexible JavaScript Modal Popup Window
Image 1

Introduction

Nowadays, JavaScript Modal Popup window has become an intrinsic part of Web based application. Lately, I had a requirement where I needed to use Modal Popup Window. I Goggled for it and found many JQuery based Modal Popup Windows, but none of the code was fitting my needs.

So I decided to create my own Modal Popup window without using JQuery which should be easy to use and is flexible.

Using the Code

First, you need include ModalPopupWindow.js file in your web page as given below:

JavaScript
<script src="JavaScript/ModalPopupWindow.js"
type="text/javascript"></script>

Once you have included the JS file, you need to create an instance of ModalPopUpWindow. For that, you need to write the below mentioned code:

JavaScript
modalWin = new CreateModalPopUpObject();

modalWin object provides three functions to create three types of modal windows which are mentioned below:

  • ShowMessage
  • ShowConfirmationMessage
  • ShowURL

ShowMessage

ShowMessage can be used when you want to show some information to the user:

Image 2

JavaScript
ShowMessage(message, height, width, title)

ShowMessage function takes four parameters:

  • Message: Message that you need to display to the user
  • Height: Height of the message window
  • Width: Width of message window
  • Title: Title of the window

Syntax for calling is as follows:

JavaScript
modalWin.ShowMessage('This Modal Popup Window using Javascript',200,400,'User Information');

ShowConfirmationMessage

ShowConfirmationMessage window can be used where you want to take confirmation from user:

Image 3

JavaScript
ShowConfirmationMessage(message, height,
            width, title, onCloseCallBack, firstButtonText, onFirstButtonClick, secondButtonText,
            onSecondButtonClick)

ShowConfirmationMessage function takes eight parameters:

message, height, width, title parameters are already described above.

  • onCloseCallBack (optional): Name of the function that will get called on confirmation window close
  • firstButtonText: Text that needs to be displayed on first button (like Yes, Agree, OK, etc.)
  • onFirstButtonClick: Name of the function that will get called on click of first button.
  • secondButtonText: Text that needs to be displayed on second button (like Yes, Agree, OK, etc.)
  • onSecondButtonClick: Name of the function that will get called on click of second button

Syntax for calling is as given below:

JavaScript
modalWin.ShowConfirmationMessage('This
           Modal Popup Window using Javascript',200,400,'User Confirmation',
           null,'Agree',Action1,'Disagree',Action2);

ShowURL

ShowURL function can be used when you want to display a new page or URL in Modal popup window.

Image 4

JavaScript
ShowURL(url, height, width, title,
           onCloseCallBack, callbackFunctionArray)

height, width, title, onCloseCallBack are already described above.

  • url: Location or URL of the web page
  • callbackFunctionArray (optional): Using this parameter, you can pass array of function of parent page to the child page inside Modal Window. So that child page can access function of parent page.

Syntax for calling is as follows:

JavaScript
var callbackFunctionArray = new Array(EnrollNow,
EnrollLater);

modalWin.ShowURL('Child.htm',320,470,'User
Enrollment',null,callbackFunctionArray);   

SetButtonStyle

You can change the button style using function SetButtonStyle. Sometimes, you may want to show button as link, in that case you can use this function. Below is an example to show buttons as link.

Image 5

JavaScript
SetButtonStyle(btnStyle)
  • btnStyle: is semicolon separated styles for buttons. The naming convention for the styles should be like JavaScript not like CSS style. For example in CSS, we use "text-decoration" and in JavaScript, we use "textDecoration". So, please make sure while passing styles that you are using JavaScript naming convention.

Syntax for calling is as given below:

JavaScript
modalWin.SetButtonStyle("background:none;border:none;textDecoration:underline;cursor:pointer");

Dragging Window

By default ModalPopUpWindow is draggable, for dragging window you need press mouse over window title bar and drag. In case you don’t want window to be draggable, then you can use Draggable property of ModalPopUpWindow. You need to set this property every time just before calling any show function of ModalPopUpWindow to make it un-draggable

Image 6

Syntax for making window un-draggable is given below:

JavaScript
modalWin.Draggable=false;
modalWin.ShowMessage('You can not drag this window',200,400,'User Information');

Passing Values From Modal Popup Child Window To Parent Window

Sometimes, we may need to pass values from child modal window to parent window while using "ShowURL" function of modal window. In JavaScript, we can use window.parent for accessing parent window elements/functions. So we can use the same (window.parent) for passing value from child popup window to parent window. You can find example of the same in the attached demo code where clicking on "Show URL Window" will show up popup window and when you will click on "Enroll Now" button, it will pass values of child window to parent window as shown in below screenshots:

Image 7

Image 8

Summary

In the attached source code, you will find examples for all the scenarios described in this tip. Apart from that, you can modify the source code depending on your requirement.

History

  • Updated source code version v4 for supporting IE11 on 20-Nov-2013

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Developer

Comments and Discussions

 
GeneralRe: Does not works in Google Chrome Pin
Member 196873018-May-14 15:24
Member 196873018-May-14 15:24 
GeneralRe: Does not works in Google Chrome Pin
Member 109913745-Aug-14 8:22
Member 109913745-Aug-14 8:22 
GeneralRe: Does not works in Google Chrome Pin
Member 196873018-May-14 15:32
Member 196873018-May-14 15:32 
GeneralRe: Does not works in Google Chrome Pin
Mahesh Bailwal18-May-14 18:58
Mahesh Bailwal18-May-14 18:58 
Questionshowing multiple url Pin
rajeshmsen2-May-14 3:06
rajeshmsen2-May-14 3:06 
AnswerRe: showing multiple url Pin
Mahesh Bailwal3-May-14 2:47
Mahesh Bailwal3-May-14 2:47 
QuestionHow to call window modal with button server side (asp.net)? Pin
carlosrenatofreire19-Apr-14 1:08
carlosrenatofreire19-Apr-14 1:08 
AnswerRe: How to call window modal with button server side (asp.net)? Pin
Mahesh Bailwal19-Apr-14 3:34
Mahesh Bailwal19-Apr-14 3:34 
Please check following links on calling JavaScript from server side button click.

How to Call javascript function on button click event[^]

alling javascript function from button click event [^]

http://www.w3schools.com/aspnet/prop_webcontrol_button_onclientclick.asp[^]

Once you understand the mechanism for calling JavaScript from server side code, than you can use same for calling modal window from server side button click.

Hope it will help Smile | :)
QuestionCan I change the popup CSS ? Pin
Member 1063894110-Apr-14 1:00
Member 1063894110-Apr-14 1:00 
AnswerRe: Can I change the popup CSS ? Pin
Mahesh Bailwal11-Apr-14 3:43
Mahesh Bailwal11-Apr-14 3:43 
QuestionOnUrlLoaded Error Pin
Jeneel Barzaga Frani20-Mar-14 1:08
Jeneel Barzaga Frani20-Mar-14 1:08 
AnswerRe: OnUrlLoaded Error Pin
Mahesh Bailwal20-Mar-14 3:58
Mahesh Bailwal20-Mar-14 3:58 
GeneralRe: OnUrlLoaded Error Pin
Jeneel Barzaga Frani20-Mar-14 16:15
Jeneel Barzaga Frani20-Mar-14 16:15 
QuestionRefresh the data on the parent page after closing the child page? Pin
tmschneider6-Mar-14 10:09
tmschneider6-Mar-14 10:09 
AnswerRe: Refresh the data on the parent page after closing the child page? Pin
Mahesh Bailwal6-Mar-14 23:29
Mahesh Bailwal6-Mar-14 23:29 
QuestionShowURL appears as a normal window, not a modal Pin
Member 105829519-Feb-14 7:12
Member 105829519-Feb-14 7:12 
AnswerRe: ShowURL appears as a normal window, not a modal Pin
Mahesh Bailwal9-Feb-14 19:11
Mahesh Bailwal9-Feb-14 19:11 
GeneralRe: ShowURL appears as a normal window, not a modal Pin
Member 1058295111-Feb-14 5:09
Member 1058295111-Feb-14 5:09 
GeneralRe: ShowURL appears as a normal window, not a modal Pin
Mahesh Bailwal11-Feb-14 5:37
Mahesh Bailwal11-Feb-14 5:37 
GeneralRe: ShowURL appears as a normal window, not a modal Pin
Member 1058295114-Feb-14 8:41
Member 1058295114-Feb-14 8:41 
GeneralRe: ShowURL appears as a normal window, not a modal Pin
Mahesh Bailwal14-Feb-14 18:53
Mahesh Bailwal14-Feb-14 18:53 
Questionis it possible to store and fetch details from database using c# Pin
kishorebt4-Feb-14 1:24
kishorebt4-Feb-14 1:24 
AnswerRe: is it possible to store and fetch details from database using c# Pin
Mahesh Bailwal4-Feb-14 2:52
Mahesh Bailwal4-Feb-14 2:52 
GeneralRe: is it possible to store and fetch details from database using c# Pin
kishorebt4-Feb-14 17:39
kishorebt4-Feb-14 17:39 
GeneralRe: is it possible to store and fetch details from database using c# Pin
Mahesh Bailwal4-Feb-14 22:15
Mahesh Bailwal4-Feb-14 22:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.