Tips & Tricks - Set and read cookies

This workshop demonstrates how cookies can be created and how values can bed saved and then read with JavaScript. To do this, a welcome screen will be created where users can decide whether they want to see this again, when they open the start page the next time.

When the application is loaded in the browser, the desired notice is now shown. Please note that these are not database entries, but non-persistent cookies. This means that the desired settings are lost if cookies are deleted in the browser.

You can download the sample application here and import it as usual. Activate the expert options so that all dialogs mentioned in this example are accessible. Knowledge of JavaScript is an advantage.

You will find the "Welcome" view page in the application. This page should be loaded in the browser in a tooltip as a welcome screen. Below the information, which you can of course design as you wish, there is a checkbox that has no link to a data field. Its title is "Do not show note again". If you switch to the "Script" tab in the properties dialog of the checkbox and open the JavaScript editor, you will 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 is used to read the value of the checkbox and write a cookie with the key "display.dont.show.again" and the corresponding value. Finally, the validity date of the cookie is defined in milliseconds. In this case, the cookie expires after a year. If you want to use the script in other applications, replace the GUID (FA74C33D46BB032DE215576A4540AE14C7E7F8F1) of the checkbox with the GUID of the checkbox 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 on the checkbox in the browser.

On the start page of the application, you will 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 side. The "Welcome" page is selected as the jump target for the button.

The positioning and size of the tooltip can be set in the landing page options.

If you open the script editor again on the "Script" tab, you will 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. Based on this, the function delivers true or false as the return value. The function ""displayIntro()"" calls ""showIntro()"" to query 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 on the start page in the hidden area is clicked. If you want to use the script in other applications, replace the >GUID (001547BDDC66F889B3D82E2C7321D73708CF58D1) of the button with the GUID of the button from your application.

In the "Script" tab of the start page properties dialog, the displayIntro() method is assigned to the onload event of the page.