MooTools provides us with Cookie, a global object that provides us with a clean and simple access to the cookies on our web browser.

The following code sample shows how simple it is to create a new cookie and how simple it is to read the value of a cookie that already exists.

<!DOCTYPE html>
<html>
<head>
    <title>cookie demo</title>
    <script type="text/javascript" src="mootools-core-1.4.5-full-nocompat.js"></script>
    <script type="text/javascript">
        function saveCookie()
        {
            Cookie.write(
                    document.myform.savekey.value,
                    document.myform.savevalue.value,
                    {duration:1}
                );
        }
        function getCookie()
        {
            var key = document.myform.getkey.value;
            document.myform.getvalue.value = Cookie.read(key);
        }
    </script>
</head>
<body>
    <form name="myform">
        <p>
            key: <input type="text" name="savekey"/>
            <br/>
            value: <input type="text" name="savevalue" />
            <br/>
            <input type="button" value="save" onClick="saveCookie()"/>
        </p>
        <p>
            key: <input type="text" name="getkey"/>
            <br/>
            <input type="button" value="get" onClick="getCookie()" />
            <br/>
            <input type="text" name="getvalue"/>
        </p>
    </form>
</body>
</html>

The following video clip overviews this code sample, shows its execution and explains each and every part of it.

You can find more video clips, code samples and presentations for learning how to use the MooTools JavaScript framework in my free courses web site at http://abelski.lifemichael.com.

Leave a Reply

Your email address will not be published. Required fields are marked *