Paul McFedries' Web Home


Passing Parameters II: Using Cookies to Load a Selected Image in a Boilerplate Page

This page shows you how to use cookies to pass a parameter to another Web page. In this example, the user clicks an image and then a full-size version of the selected image is loaded into a boilerplate page. Here are the images:


Click an image to load the full-size version:
Paul McFedries' Windows 95 Unleashed Paul McFedries' Office 97 Unleashed The Complete Idiot's Guide Creating an HTML Web Page The Complete Idiot's Guide to Windows 95

To set this up, you must do four things:
  1. Copy the following JavaScript code and insert it in your page between the </HEAD> and <BODY> tags:
    <SCRIPT LANGUAGE = "JavaScript">
    <!--
        // This function sets a cookie named "Image".
        // The value of the cookie is the name of the 
        // graphics file you want to load in the boilerplate page.
        //
        function BakeIt(img) {
            //
            // Use this variable to set the name of the cookie
            // 
            var cookieName = "Image";
            // 
            // Use this variable to set the number of days after which the cookie will expire
            //
            var days = 1;
            //
            // Calculate the expiration date
            //
            var expires = new Date ();
            expires.setTime(expires.getTime() + days * (24 * 60 * 60 * 1000)); 
            //
            // Set the cookie
            //
            SetCookie(cookieName, img, expires);
        }
    
        function SetCookie(cookieName, cookieData, expireDate) {
            document.cookie = cookieName + "=" + escape(cookieData) + "; expires=" + expireDate.toGMTString();
        }    
    //-->
    </SCRIPT>
    
  2. Modify each link with the following JavaScript snippet:

    onClick="BakeIt('filename')"

    Here, filename is the name of the image to be loaded in the boilerplate page. Here's an example of a complete link:

    <A HREF="boilerplate2.asp" onClick="BakeIt('win95pre-full.jpg')">


Copyright © 1995-2008 Paul McFedries and Logophilia Limited