Integrating with the Modal Dialog library

Documentation about using the Modal Dialog library that comes with RGraph. In version 7.21 (released in summer 2026) the Modal Dialog library was completely rewritten, improved, updated to modern coding techniques and made to use modern browser features.

Introduction

This page shows you how to make use of the Modal Dialog library that's bundled with RGraph. The Modal Dialog has been part of RGraph for many years - but in version 7.21 (summer 2026) it was entirely rewritten and modernised to make use of better and more reliable techniques. A lot of now redundant, backwards-compatibility code was removed. As a result, it's not 100% compatible with the prior version but the updated code is more efficient and more reliable which makes upgrading to it well worth it.

The example below allows you to click on the button to show the dialog which is configured as a login form. This could be used to allow logging in to a user account or an admin area.

Note that you don't have to use RGraph in order to use the Modal Dialog - the library can be used standalone without involving RGraph if that's what you want.

The dialog that you show doesn't need to require user input - it could just be a static "Please wait..." type dialog, which is shown while a subsequent page loads, which takes a few seconds. Another use-case is to show more information to the user - an example of which is the Help link on the forum search page.

The Modal Dialog confirmation function

Also added in the new version, was the ModalDialog.confirm function. This serves as a replacement for the browser-native confirm and prompt functions. Using this function allows you to create pretty confirmation dialogs and prompts which call JavaScript functions (of your choosing) when the OK or Cancel buttons are pressed. More on the Modal Dialog confirm functionality below.

The Modal Dialog was originally an external library, however it's now bundled as a part of RGraph.

Example

Shown below is an example of the Modal Dialog that can be seen by clicking on the button. This shows the Modal Dialog being used to show a login form.

There's another example of using the Modal Dialog to show the help information that you can see on the forum search page (click on the help link).

<script src="RGraph.modaldialog.js"></script>

<p style="text-align: center">
    <button id="myButton" style="font-size: 20pt; cursor: pointer">Login...</button>
</p>

<script>
    document.getElementById("myButton").onclick = function ()
    {
        //
        // This is the code that creates the Modal Dialog
        //
        ModalDialog.show({
            id: 'myDialog',
            options: {
                zoomFactorStart: 1,
                zoomFactorEnd: 1,
                rotationStart: '-45deg',
                rotationEnd: '-45deg',
                skewStart: '-90deg, -90deg',
                skewEnd: '-90deg, -90deg',
                translateStart: '-250px , -250px',
                translateEnd: '-250px, -250px',                
                effectShowDuration: 750,
                effectHideDuration: 750,
                style: [
                    '.ModalDialog_dialog {font-family: Verdana, sans-serif; line-height: 35px;padding: 20px !important;}',
                    '.ModalDialog_dialog td {font-size: 16pt !important;}',
                    '.ModalDialog_dialog input[type=reset],input[type=submit],input[type=text],input[type=password] {cursor: pointer;font-size: 16pt;}',
                    '.ModalDialog_dialog b {font-size: 22pt; font-style: italic;}'
                ]
            }
        });
    };
</script>

<!--
    This is the popup dialog (an alternative is to supply the HTML
    that the dialog uses as a string instead of the id of a <div> tag.
-->
<div id="myDialog" class="myDialog" style="display: none">
    <b>Please login</b>
    <table border="0">
        <tr>
            <td align="right" style="padding-top: 4px">Email:</td>
            <td><input type="text" name="email" style="width: 150px" /></td>
        </tr>
        <tr>
            <td align="right" style="padding-top: 4px">Password:</td>
            <td><input type="password" name="password" style="width: 150px" /></td>
        </tr>
        <tr>
            <td colspan="2" align="right">
                <input type="reset" value="Cancel" onclick="ModalDialog.hide()">
                <input type="submit"
                          name="submit"
                          value="Login »"
                          onclick="alert('This is just an example'); event.stopPropagation()">
            </td>
        </tr>
    </table>
</div>
<!-- End of dialog -->
</script>

Hiding the Modal Dialog

To hide the Modal Dialog (from a "Cancel" button in your dialog for example), you can use the ModalDialog.hide method:

<button type="button" onclick="ModalDialog.hide()">Cancel</button>

Modal Dialog configuration properties

Name: zoomFactorStart
Description: 
When the Modal Dialog is shown, by default, there's a zoom effect used. This setting controls the starting zoom factor and thus how much the dialog zooms. The default setting of zero poduces an elargement effect, a setting of 1 means that effectively there is no zoom and a setting of 2 means that the dialog starts large and then shrinks to the correct size.
Default:  0
Name: zoomFactorEnd
Description: 
When the Modal Dialog is hidden, by default, there's a zoom effect used. This setting controls the ending zoom factor and thus how much the dialog zooms. The default setting of zero poduces a shrinking effect, a setting of 1 means that effectively there is no zoom and a setting of 2 means that the dialog expands whilst fading out.
Default:  0
Name: rotationStart
Description: 
By default, the Model Dialog will rotate slightly when it's being displayed. Using this property you can specify a CSS string that controls the starting angle of the dialog and thus, how much it will spin. The dialog will transition to an angle of zero - so if you set this to a bigger angle you'll see more spinning. This should be a valid CSS angle with either degrees (deg) or radians (rad) as units.
Default:  45deg
Name: rotationEnd
Description: 
This property controls the ending angle that the Modal Dialog transitions to when being hidden. When it's displayed, it's at zero degrees so if you set this property to a higher angle you'll see more spinning when the dialog is hidden. This should be a valid CSS angle with either degrees (deg) or radians (rad) as units.
Default:  45deg
Name: translateStart
Description: 
To see the Modal Dialog slide in when being displayed you can set this property to the desired origin coordinates. It should be a pair of valid CSS values with units separated by a comma. For example: 0px, -200px. The only units supported are pixels (px).
Default: 0px, 0px
Name: translateEnd
Description: 
To see the Modal Dialog slide out when being hidden you can set this property to the desired target coordinates. It should be a pair of valid CSS values with units separated by a comma. For example: 0px, -200px. The only units supported are pixels (px).
Default: 0px, 0px
Name: skewStart
Description: 
Skewing is a transformation that permits you to "slant" the dialog at the start and end of the display and hide effects. The dialog is animated to zero skew when it's displayed. It can be either a single CSS angle (with units, for example "90deg" or "2rad" - if you omit the units then degrees are assumed) in which case the dialog is skewed in just the X direction or it can be two CSS angles separated by a comma (with units), for example: "-45deg, -45deg" and the dialog will be skewed in both the horizontal and vertical directions.
Default: 0deg, 0deg
Name: skewEnd
Description: 
This property stipulates the ending skew angle that the dialog transforms to when it's being hidden. It can be either a single CSS angle (with units - if you omit the units then degrees are assumed) or two CSS angles (with units) separated by a comma (see the example above in the skewStart description).
Default: 0deg, 0deg
Name: opacityStart
Description: 
By default, the dialog will fade in when it's displayed. With this property (a number between zero and one), you can control the initial setting of the opacity (how transparent the dialog is). If you don't want the dialog to fade in at all you can set this to 1.
Name: opacityEnd
Description: 
By default, the dialog will fade out when it's hidden. With this property (a number between zero and one), you can control the final setting of the opacity (how transparent the dialog is). If you don't want the dialog to fade out at all you can set this to 1.
Name: hideOnBackground
Description: 
By default, when the semi-opaque background is clicked it hides the dialog (if you click the dialog itself it doesn't though). This option allows you to disable that if you prefer. You can still press the esc key on the keyboard though to hide the dialog.
Default: true
Name: pageScroll
Description: 
By default, when the Modal Dialog is shown the page scrollbars are disabled so the page cannot be scrolled using the mouse wheel. If you want to change this behaviour so that you can scroll the page whilst the Modal Dialog is displayed, then set this property to true.
Default: false
Name: style
Description: 
The style property is an easy way to add any style specifications for the Modal Dialog without having to add them separately into your page or in your CSS file - you can keep the configuration of the dialog and the styles that it uses all in a single place. Here's an example of how you might use this property:
...
style: [
    '.ModalDialog_dialog {line-height: 25px;box-shadow: 3px 3px 3px gray !important; border: none !important;padding: 15px !important;font-size: 20pt;}',
    '.ModalDialog_dialog i {font-size: 12pt}',
    '.ModalDialog_dialog :where(input[type=text], input[type=password]) {width: 250px; font-size: 16pt}',
    '.ModalDialog_dialog :where(input[type=submit], input[type=reset]) {font-size: 16pt}',
    '.ModalDialog_dialog :where(input[type=reset], input[type=submit]) {cursor: pointer;}',
    '.ModalDialog_dialog input[type=submit] {background-color: blue; color: white; border: none; padding: 5px;}',
    '.ModalDialog_dialog input[type=reset] {opacity: 0.5;}'
]
...

Note that the styles that you give are only added to the document when the Modal Dialog is shown - in a style tag at the end of the head section of the document. When the dialog is hidden this style tag is then removed.

Also note that the selectors that you specify automatically have the string "div#ModalDialog_container_xxx" added to the start of them (the _xxx part is actually a long random number). You don't need to (and should not) add this yourself. This is to try and help with preventing CSS conflicts.

Default: [an empty array]
Name: effectShowDuration
Description: 
This is the duration (in milliseconds) that the show effect takes. One second is equal to 1000 milliseconds so half a second is 500 milliseconds.
Default: 500
Name: effectHideDuration
Description: 
This is the duration (in milliseconds) that the hide effect takes. One second is equal to 1000 milliseconds so half a second is 500 milliseconds.
Default: 500
Name: position
Description: 
By default, the Modal Dialog is positioned in the center of the page, but this doesn't have to be the case. Using this option, you can set the dialog to be in a few different positions. It should be two separate words - [hpos], [vpos] - which can be left, center or right (for the horizontal position) and top, center or bottom (for the vertical position).

As well as words, this can also be a pixel coordinate (again, one for horizontal and one for vertical) like this: 100px, 50px. You can also use percentages instead of pixels if you wish: 50%, 50% and you can also mix them up if you want as well, like this: center, 100px or this: 50px, 50%.

Default: center center
Name: align
Description: 
This property allows you to specify how the dialog is aligned both horizontally and vertically. Similar to the position property above, the value of the align property can be two words, the first word being the horizontal alignment (left, center or right) and the second word being the vertical alignment (top, center or bottom).
Default: center center
Name: zIndex
Description: 
This property allows you to control the CSS z-index setting of the Modal Dialog container DIV. You will need to set this if you need to set the Modal Dialog to have a higher stacking order than the default of 10000. You can also set this with an entry in the styles property, for example (note that there's no selector given - this means that the rule gets applied to the Modal Dialog container DIV element):
style: [
    '{z-index: 10005 !important;}'
]
Default: 10000

Customising the Modal Dialog

You can customise the appearance of the Modal Dialog by using the style property which is mentioned above in the API documentation, There's an example of using the style property to customise the Modal Dialog in the demos in the download archive . Note that the selectors that you give are automatically prefixed with .ModalDialog_container_xxx (where_xxx is a number) in order to restrict the CSS to just the Modal Dialog being shown. When the Modal Dialog is hidden the style element that was added is automatically removed.

Note: If you're trying to set styles which are set later in the document by other stylesheets or by the Modal Dialog library itself you may need to use the !important declaration in your rule like this:

style: [
    '.ModalDialog_dialog {font-size: 24pt !important;}'
]

Modal Dialog integration

To integrate the Modal Dialog look at the sample code above (the key section is where the onclick event listener is defined). The method that you should call is ModalDialog.show({id: 'myDialog', options: {...}}). The id is the id of the DIV tag to use. Only the innerHTML is used, not the DIV itself, so it can be hidden by setting the CSS display property to none. The width of the dialog can be set, if you want, by using CSS. Also, you can give the HTML to use as a string instead of the ID of a hidden DIV tag in the page (see below).

The only library needed for the Modal Dialog to work is RGraph.modaldialog.js - you do not need to use RGraph.common.core.js. This makes for far smaller download requirements for your users.

Supplying the dialog as a string

Normally, having forms in your dialog isn't a problem. However, if you want to access the form elements with JavaScript it can be problematic because there are two form elements with the same ID - the original, hidden one, and also the one in the Modal Dialog when it's shown. Because of this, you can supply the dialog as a string, like this:

<script>
    str = 'string:<h1>My Dialog</h1><p align="center"><button onclick="ModalDialog.hide();">Close</button></p>';
    
    ModalDialog.show({
        id: str,
        options: {
        }
    });
</script>

Also, you may find this method easier to use if you're showing multiple dialogs on your page.

Note: Because this is much easier than having the HTML embedded in your page, this is now the recommended way to show dialogs.

Using the Modal Dialog as a confirmation promptNew!

See examples of using the Modal Dialog to show confirmation dialogs here.

This is new in version 7.21 and allows you to easily use the Modal Dialog as a replacement for the built-in, but far less attractive, JavaScript confirm, prompt and alert functions.

It's not possible to block execution when showing a dialog box like the confirm, prompt or alert functions do but what can be done instead is to give a function that is run when the OK button (or the cancel button) in the dialog is pressed. Doing this also allows you to fetch the values of any form inputs that you may have in your confirmation dialog.

The example code below shows a simple dialog box which has OK and Cancel buttons. When the OK button is pressed the callbacks.ok callback function is run and when the Cancel button is pressed the callbacks.cancel callback function is run. You don't have to give a callbacks.cancel function though - the dialog will just be hidden and removed from the DOM if there isn't a function defined.


<script>
    function showConfirmationDialog ()
    {
        ModalDialog.confirm({
            text: 'Are you <b>sure</b> that you want to do that?',
            callbacks: {
                ok:     function () {alert('OK button was pressed');},
                cancel: function () {alert('Cancel button was pressed');}
            }
        });
    }
</script>

<p>
    <button type="button" onclick="showConfirmationDialog()">Click me</button>
</p>