Monday, April 8, 2013

My solution to chapter 12 exercises of JavaScript Step by Step 2e

1. Create a webpage that sends a cookie to the browser. Set the expiration date ahead
one day. Verify that the JavaScript code sent the cookie to the browser by viewing it as
it gets set or after it’s been stored on the computer. You could accomplish this second
part of the exercise; by using JavaScript or by viewing the cookies on the computer.

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script>
        var cookieName = "mycookie-1";
        var cookieValue = "mycookievalue-1";
        var date = new Date();
        date.setTime(date.getTime() + (24 * 60 * 60 * 1000));
        var expireDate = date.toGMTString();

        var myCookie = cookieName + "=" + cookieValue + ";expires=" + expireDate;
        document.cookie = myCookie;

        // the following code is not part of exercise 1
        // another cookie
        var cookieName2 = "mycookie-another";
        var cookieValue2 = "mycookievalue-another";
        var date2 = new Date();
        date2.setTime(date2.getTime() + (24 * 60 * 60 * 1000));
        var expireDate2 = date2.toGMTString();

        var myCookie2 = cookieName2 + "=" + cookieValue2 + ";expires=" + expireDate2 + ";path=/;domain=localhost;";
        document.cookie = myCookie2;
       
        alert(document.cookie);
    </script>
</body>
</html>


2. Create a webpage that sends a cookie with the cookie’s expiration date set ahead one
week, and set the secure flag. This page can be the same one you created for Exercise
1, but be sure to give the cookie a different name so that you’ve created two separate
cookies, one for each exercise. Also, be sure to enable the secure flag for the cookie in
this exercise, not for the cookie in Exercise 1.

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script>
        var cookieName = "mycookie-2";
        var cookieValue = "mycookievalue-2";
        var date = new Date();
        date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000));
        var expireDate = date.toGMTString();

        var myCookie = cookieName + "=" + cookieValue + ";expires=" + expireDate + ";secure";
        document.cookie = myCookie;
    </script>
</body>
</html>

3. Create a webpage that attempts to read the cookie with the secure flag set. Did you
receive the cookie? If not, what would you need to do to receive it?

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script>
        var cookies = document.cookie.split(";");
        var cookLength = cookies.length;
        for (var c = 0; c < cookLength; c++) {
            alert(cookies[c]);
        }
    </script>
</body>
</html>

4. Create a webpage that reads the cookie you created in Exercise 1. Use a for loop and an
if conditional to display an alert() dialog box when the cookie with the correct name is
found within the loop. Don’t display an alert() dialog box for any other cookies.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script>
        var cookies = document.cookie.split(";");
        var cookLength = cookies.length;
        for (var c = 0; c < cookLength; c++) {
            var pairs = cookies[c].split("=");
            var cookieName = pairs[0];
            var cookieValue = pairs[1];

            // display only the cookie with name of "mycookie-1"
            if (pairs[0] === "mycookie-1") {
                alert("Name: " + cookieName + " -> " + "Value: " + cookieValue);
            }
        }
    </script>
</body>
</html>

No comments:

Post a Comment