Tips & Tricks - Setting and Reading Cookies

This post shows how to create cookies using JavaScript, store values in them, and read those values back out. To do this, a welcome screen is created where users can choose whether or not they want it to appear again the next time they visit the home page.

When the application is opened in the browser, the desired message is now displayed. Please note that these are not database entries, but rather non-persistent cookies. This means that your preferred settings will be lost if you clear your browser's cookies.

You can download the sample application here and import it as usual. Enable the advanced options so that all the dialog boxes mentioned in this example are accessible. Knowledge of JavaScript is helpful for this post.

In the application, you'll find the "Welcome" page. This page should be displayed in the browser as a welcome screen in a tooltip. Below the information—which you can, of course, customize as you like—there is a checkbox that is not linked to any data field. Its title is "Do not show this message again." If you go to the "Script" tab in the checkbox's Properties dialog box and open the JavaScript editor, you'll find the following script:

            function setDisplayIntro()
{
    var bDisplay = Browser.getValue("FA74C33D46BB032DE215576A4540AE14C7E7F8F1");
    ix.cookie.setValue(
        "display.dont.show.again",
        bDisplay,
        true,
        365 * 24 * 60 * 60 * 1000
    );

    return true;
}

        

This function reads the value of the checkbox and writes a cookie with the key "display.dont.show.again" and the corresponding value. Finally, the cookie's expiration date is set in milliseconds. Here, the expiration date is set to one year. If you want to use the script in other applications, replace the GUID (FA74C33D46BB032DE215576A4540AE14C7E7F8F1) of the check box with the GUID of the check box from your application. Close the editor again. The script is assigned to the onclick event on the "Script" tab. It is triggered when a user clicks the checkbox in the browser.

On the app's home screen, you'll find the "Show Welcome Screen" button in the hidden area. Here, the checkbox in the "Web" column is selected so that the element is available on the web and not just on the server. The "Welcome" page is selected as the button's destination.

You can adjust the position and size of the tooltip in the landing page options.

If you open the script editor again from the "Script" tab, you'll see the following two functions:

            function displayIntro()
{
    if (showIntro()) {
        ix.util.getElement("001547BDDC66F889B3D82E2C7321D73708CF58D1").click();
    }

    return true;
}

function showIntro()
{
    var strCookieName = "display.dont.show.again";
    var strDontShowAgain = ix.cookie.getValue(strCookieName) || null;

    if (!strDontShowAgain || strDontShowAgain === "0" || strDontShowAgain === "false") {
        return true;
    }

    return false;
}

        

The "showIntro()" function reads the value stored in the "display.dont.show.again" cookie. Depending on that, the function returns either true or false. The displayIntro() function calls showIntro() to determine whether the welcome screen should be displayed. If the result of this query is true (i.e., showIntro() returns true), the button that was created in the hidden area on the home page is clicked. If you want to use the script in other applications, replace the >GUID (001547BDDC66F889B3D82E2C7321D73708CF58D1) for the button with the GUID of the button in your application.

In the Properties dialog for the home page, on the "Script" tab, the displayIntro() method is assigned to the page's onload event.