Jquery dialog should only open once per user

javascript, jquery, jquery-ui-dialog

Solution

Either use `cookies` (like mentioned before) or the `localStorage` API (depending on which browsers you have to support).

A way with cookies:

$(function() {
    if( document.cookie.indexOf( "runOnce" ) < 0 ) {
        $( "#dialog-message" ).dialog({
            modal: true,
            resizable: false,
            show: 'slide',
            buttons: {
                Ok: function() {
                    $( this ).dialog( "close" );
                    document.cookie = "runOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/";
                }
            }
        });
    }
});

A way with localStorage:

$(function() {
    if( ! localStorage.getItem( "runOnce" ) ) {
        $( "#dialog-message" ).dialog({
            modal: true,
            resizable: false,
            show: 'slide',
            buttons: {
                Ok: function() {
                    $( this ).dialog( "close" );
                    localStorage.setItem( "runOnce", true );
                }
            }
        });
    }
});

Problem

I use jquery ui modal dialog on a front page and I'd like to show it only one per user. This is my code: ``` <script> $(function() { $( "#dialog-message" ).dialog({ modal: true, resizable: false, show: 'slide', buttons: { Ok: function() { $( this ).dialog( "close" ); } } }); }); </script> <div id="dialog-message" title="Attention"> <p>Sample text here!</p> </div> ``` Any help is much appreciated! Thanks!

Original source