Easy to use Javascript dialog box, messagebox, alert, confirm, prompt, toast.
Plain vanilla Javascript, no libraries.
Customizable, responsive, draggable, resizable, lightweight.
Using standard Javascript (ES5), HTML and CSS.
Crossbrowser, legacy browser support as much as possible.
Include the CSS and Javascript files between <head>
and </head>
as you would normally do in a HTML file:
Then it can be used like the examples below. Boxes can also be created with pre-written HTML.
All dialogboxes are modal by default, but this can be changed among other things, by using optional flags.
Displaying multiple boxes at the same time is also possible, or how about a Toast!
EasyDialogBox
As long as the script-file is included as above then the module will be initialized automatically when page loads, after that all the functions and methods are available.
Minimum syntax:
let myObj = EasyDialogBox.create(id, strBoxTypeClass, strTitle, strMessage);
Full syntax:
let myObj = EasyDialogBox.create(id, strBoxTypeClass, strTitle, strMessage, fnCallback, x, y, w, h);
Return value = object, or null if could not be created.
Parameters:
id
= custom id 'string' , if not used then you must set it to a false
, null
or 0
(zero)
strBoxTypeClass
= can be any of the strings in the table below, by itself or combined with others separated by a space:
Dialogtype | Description |
---|---|
dlg | Default dialog with Close button. |
dlg-close | Dialog with Close button, add Close button to dialog. |
dlg-prompt | Dialog with an inputfield, OK + Cancel buttons. |
dlg-yes | Dialog with Yes button, add Yes button to dialog. |
dlg-no | Dialog with No button, add No button to dialog. |
dlg-yes-no | Dialog with Yes + No buttons, add Yes + No buttons to dialog. |
dlg-ok | Dialog with OK button, add OK button to dialog. |
dlg-cancel | Dialog with Cancel button, add Cancel button to dialog. |
dlg-ok-cancel | Dialog with OK + Cancel buttons, add OK + Cancel buttons to dialog. |
dlg-toast |
Toast dialogbox, shorthand that combines the flags below:dlg dlg-disable-heading dlg-disable-footer dlg-disable-drag dlg-disable-overlay
|
Optional flags 1 | Description |
dlg-disable-heading | Dialog without heading. |
dlg-disable-footer | Dialog without footer. |
dlg-disable-btns | Dialog without any buttons in the footer. |
dlg-disable-drag | Disable drag'n'drop, dialogbox cannot be moved by dragging its heading. |
dlg-disable-esc | Disable Esc-key so it won't close the box. |
dlg-disable-clickout | Disable the ability to click outside the box to close it. |
dlg-disable-overlay | Disable translucent overlay/background color and makes the dialogbox modeless. |
dlg-nomodal | Same as dlg-disable-overlay |
dlg-multi |
Makes it possible and easier to use multiple dialogboxes at the same time. Shorthand that combines the flags below: dlg-disable-overlay dlg-disable-esc dlg-disable-clickout Note: IE 8/9/10 - to switch between dialogs you have to click on some text or visible element. |
dlg-resize | Makes the dialogbox resizable, adds a small resize-handle on the bottom-right side. |
dlg-info | Display information icon on the left inside box. |
dlg-question | Display question icon on the left inside box. |
dlg-error | Display error icon on the left inside box. |
dlg-success | Display success icon on the left inside box. |
dlg-exclamation | Display exclamation icon on the left inside box. |
dlg-rounded | Add rounded corners to box. |
dlg-shadow | Add shadow behind box. |
dlg-fade | Dialogbox fades in when shown, not when closed, only fades out if used with timed hiding |
1. must be used in combination with a dialogtype, separated by a space.
strTitle
= title text of the box, text only recommended, but can contain HTML.
strMessage
= HTML content to custom and build as you wish, images, forms, anything you want, or just plain text.
Optional parameters:
fnCallback
= callback function that executes when box closes (or you can just use myObj.onClose()
function instead)
If using full syntax, but not using the fnCallback
parameter, then you must set it to a false
, null
or 0
(zero)
x
= initial horizontal position of the box. If not used then you can set it to null
and it defaults to horizontal center in viewport.
y
= initial vertical position of the box. If not used then you can set it to null
and it defaults to vertical center in viewport.
w
= initial width of the box. If not used then you can set it to null
and it defaults to 600px as defined in the css-file.
h
= initial height of the box. If not used then you can set it to null
and the height is adjusted by content.
Note: Use of x
, y
, w
, h
can possibly break the dialogbox "responsiveness".
Name | Type | Description |
---|---|---|
id | string | dialogbox ID |
strTypeClass | string | dialogbox type |
strTitle | string | dialogbox heading title |
strMessage | string | dialogbox contents (text and HTML) |
callback() | method | custom function that fires when dialog is closed (works the same way as onClose/onHide) |
x | number | dialogbox horizontal position |
y | number | dialogbox vertical position |
w | number | dialogbox width |
h | number | dialogbox height |
strInput | string | returned string value from user input when using dlg-prompt |
nRetCode | number | returns the value of the dialog-button the user clicked |
bVisible | boolean | variable that indicates if dialog is visible or not |
bExistInDOM | boolean | variable that indicates if dialog exist in DOM |
bFade | boolean | variable that indicates if dialog should fade. Using dlg-fade sets this to true |
nTimeId | number | variable that holds the timeout ID when using timed hiding |
show() | method | shows dialog |
hide() | method | hides dialog |
destroy() | method | destroys dialog |
onCreate() | method | fires when dialog is created |
onShow() | method | fires when dialog is shown |
onHide() | method | fires when dialog is hidden/closed |
onClose() | method | fires when dialog is closed |
onDestroy() | method | fires when dialog is destroyed |
el | element | reference to dialogbox element itself |
$() | method | get elements inside dialog |
width() | method | set dialogbox width (px) |
height() | method | set dialogbox height (px) |
xPos() | method | change dialogbox X position (pixels from left) |
yPos() | method | change dialogbox Y position (pixels from top) |
center() | method | position dialogbox in center of window/viewport |
colorBorder() | method | set dialog bordercolor |
colorHeading() | method | set dialog heading backgroundcolor |
color() | method | shorthand, set both heading and border same color |
addButton() | method | add custom button to dialog footer |
myObj.show();
Return value = the object itself if the box could be shown, false if not.
Default use is without any parameters: myObj.hide();
, use this if you just want to hide right away without any more fuzz.
Return value = the object itself
(Pressing the keyboard escape-key Esc also fires this function)
Optional parameters:
Else you can use it with paramters like this: myObj.hide(msec);
or this myObj.hide(msec, skip);
if using both.
msec
is a value in milliseconds of type number, to delay the hiding/closing of the dialogbox.
The timeout ID is stored in variable myObj.nTimeId
so it can be used with clearTimeout(nTimeId);
or some other custom codestuff.
skip
is a value of type boolean, to skip execution of the objects .onHide()
function. Only skips if value is true boolean, using true
, else it executes as normal which is also default.
myObj.destroy();
Return value = true if object was deleted, false if not.
All dialogboxes will be kept in the DOM by default once created, they are only removed from the DOM when the user executes the "destroy" function.
The user is responsible for deleting/destroying the dialogbox objects to prevent DOM-fill and "memory leaks".
Tip: By specifying an ID when creating a box, the existing box are reused when hiding and showing, instead of a new box being created each time the box is hidden and shown, filling up the DOM and leak memory if not destroyed.
It's good practice to destroy a dialogbox when it's not going to be used again and no longer needs to exist in DOM.
The simplest way to do this is in the functions myObj.onClose()
or myObj.onHide()
.
Like this myObj.onClose = myObj.destroy;
if nothing else is going on in the "onClose" function.
If you need to do other stuff in that function also, then you can do something like below:
myObj.el;
Return value = HTMLElement
There are multiple ways to do this, all which are similar to querySelectorAll()
myObj.$();
using () blank, returns the dialogbox HTML element itself, same as myObj.el;
, using querySelector()
myObj.$('#someId');
using # (hash), returns a single HTML element containing the specified ID, using querySelector()
, or null if none exist in DOM.
myObj.$('.someClass');
using . (period), returns a NodeList, or null if none exist in DOM.
myObj.$('someElement');
returns a NodeList, or null if none exist in DOM.
You can also mix it up e.g.:
myObj.$('#someID p input.myFormData');
returns a NodeList, or null if none exist in DOM.
myObj.$('#someID p input.myFormData')[0];
returns the first HTML element in a NodeList indexed [0]
NOTE: In all instances you can check against null to see if any elements were found.
myObj.width(value);
Where value is a pixel value of type number, e.g. myObj.width(400);
Return value = the object itself
myObj.height(value);
Where value is a pixel value of type number, e.g. myObj.height(600);
Return value = the object itself
myObj.xPos(value);
Where value is a pixel value of type number, e.g. myObj.xPos(200);
Return value = the object itself
myObj.yPos(value);
Where value is a pixel value of type number, e.g. myObj.yPos(300);
Return value = the object itself
myObj.center();
Positions the dialog in horizontal and vertical center of window or viewport.
Return value = the object itself
You can set the colors of the dialogs border and heading for some basic UI-theming without editing the CSS file or other custom CSS stuff.
myObj.color('#990000');
, to set both the heading and border color to same color.
Or you can set them individually like below:
myObj.colorBorder('#990000');
, to set the border color to one color.
myObj.colorHeading('#005599');
, to set the heading backgroundcolor to another color.
Any CSS color value will work.
To remove the custom color and go back to default you can just change the custom color value to an empty string ''
For instance from myObj.colorBorder('#ff0000');
to this myObj.colorBorder('');
, to return color back to default defined in CSS file.
Return value = the object itself
With this method you can add your own custom buttons to the footer of the dialog.
Syntax: myObj.addButton(strText, fnClick, nPos);
Where strText
is of type string, the text that you want to display on the button.
fnClick
is of type function, the click-event function for the button. Can either be a reference to a function or a function written inline.
nPos
is of type number, and is the index position of where you want to insert the button among the rest of the footer-buttons.
If set to 0
then it's inserted as the first button on the left side, if set to 1
then it's inserted as the second button from the left side, 2
as third button etc.
If omitted/not specified then the button is just appended last on the right side.
Return value = the button HTMLElement itself
myObj.onCreate();
fires when dialogbox object is created.
You can write your own function if you want to do something when the event occurs:
myObj.onShow();
fires when dialogbox is about to be displayed.
You can write your own function if you want to do something when the event occurs:
myObj.onHide();
fires when dialogbox is being hidden.
You can write your own function if you want to do something when the event occurs:
myObj.onClose();
fires when dialogbox is closing.
You can write your own function if you want to do something when the event occurs:
myObj.onDestroy();
fires when dialogbox is being destroyed.
You can write your own function if you want to do something when the event occurs:
Get object from id, can be used to get object from a pre-written HTML box or other created dialog objects by searching for its 'id' in the array of objects.
let myObj = EasyDialogBox.getById(id);
Return value = object or null if could not be found.
Get all dialogbox objects that is currently stored in the object-array.
let myArray = EasyDialogBox.getAll();
Return value = array
This is automatically done once when the script is loaded the first time. But in case there was more HTML content added later on and you need to invoke it again, you can.
It will sweep up all the boxes and activate their "click-openers".
EasyDialogBox.init();
Return value = number of pre-written HTML dialogboxes parsed.
Create a dialog and then show it.
Variables: nRetCode
and strInput
These are intended to be used in the myObj.callback()
or myObj.onClose()
function for further processing.
myObj.nRetCode
Value | Type | Description |
---|---|---|
0 | number | "Close" button, clicked outside box, clicked [X] or pressed ESC-key |
1 | number | "Yes" button clicked |
2 | number | "No" button clicked |
3 | number | "OK" button clicked |
4 | number | "Cancel" button clicked |
myObj.strInput
Value | Type | Description |
---|---|---|
string | string | A string of text entered in the <input> element when using class dlg-prompt |
Create three resizable dialogs and then show them, also giving them some color.
Used class flag dlg-multi
to make it easier to display multiple boxes at the same time, and dlg-resize
to make 2 of the boxes resizable.
Also chaining the object method execution.
You can also make "toast" notification boxes by using the flag dlg-toast
,
which is really a shorthand for the combined flags: dlg
dlg-disable-heading
dlg-disable-footer
dlg-disable-drag
dlg-disable-overlay
Using dlg-fade
to make it fade in/out.
This example creates a toast, shows it and hides it after 3 seconds. Also chaining the object methods .show()
and .hide()
.
Since not giving it an ID (just null
), I'm choosing to destroy it after hiding to clean up the DOM and memory.
The second null
is for the title, the third null
for callback function, we don't need to use them here so just set them to null
(any false will do)
Also using a null
for the X position which means center horizontally, y=20 pixels down vertically, and width=500 pixels, height not set so it adjusts according to content.
// End of Javascript object creation and examples
// Beginning of HTML examples
Create dialogboxes with HTML, no need to write any Javascript code unless you need callback()
for button clicks etc.
If you just want to display a simple messagebox with some info, then you only have to write HTML.
The rel
attribute value must contain the id
of the box that the <button>
is going to open.
( Don't have to be a <button>
, it can be any element you want to use as an opener. )
Add class dlg-opener
to the opening element, in this case a <button>
, to make it open a box, a click-event-handler will automatically be created for it.
Optional data-
attributes:
data-callback
This attribute can be used if you want to run a callback function when dialogbox is closed (no matter which button was clicked).
data-x
Custom horizontal position of the box, default is center of window.
data-y
Custom vertical position of the box, default is center of window.
data-w
Custom width of the dialogbox, default: 600px (maxWidth)
data-h
Custom height of the box, default: adjusted by content.
Class: dlg
or dlg-close
Return value: Close = 0
Classes: dlg
and dlg-error
to display icon.
Return value: Close = 0
Classes: dlg-yes-no
, dlg-question
to display icon, and dlg-fade
to fade it in.
Return value: Yes = 1, No = 2
Classes: dlg-ok-cancel
and dlg-exclamation
to display icon.
Return value: OK = 3, Cancel = 4
Class: dlg-prompt
Return value: input = string, OK = 3, Cancel = 4
Classes: dlg-prompt
dlg-close
dlg-yes-no
dlg-ok-cancel
dlg-info
dlg-disable-overlay
dlg-shadow
dlg-rounded
dlg-fade
(read more on classes)
Return value: input = string, Close = 0, Yes = 1, No = 2, OK = 3, Cancel = 4
This is a message, line one
this is line two.
This is a message
Create a new dialogbox on the fly ?
This is a message
This is a message