snippet library logo
Amazon.com
in category
Javascript / Cookies / Using a cookie
Snippet details
ID: 121
Viewed: 3784
Added: 2002-04-18
Version:
View Demo

User Rated at:
Rate This:
Snippets in this catagory         
Show Printable Version
This example shows how to set adn read a cookie by javascript. very easy to impliment adn use.

General Details
Snippet uploaded by: snippet
Email : webmaster@snippetlibrary.com
Snippet By: Webmonkey

<!---Head--->
none

<!---Body--->


<script language="JavaScript">

<!--

/*
WM_setCookie(), WM_readCookie(), WM_killCookie()
A set of functions that eases the pain of using cookies.

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Nadav Savio
Author Email: nadav@wired.com
*/


// This next little bit of code tests whether the user accepts cookies.
var WM_acceptsCookies = false;
if(document.cookie == '') {
    document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
    if(document.cookie.indexOf('WM_acceptsCookies=yes') != -1) {
        WM_acceptsCookies = true; 
    }// If it succeeds, set variable
} else { // there was already a cookie
  WM_acceptsCookies = true;
}


function WM_setCookie (name, value, hours, path, domain, secure) {
    if (WM_acceptsCookies) { // Don't waste your time if the browser doesn't accept cookies.
        var not_NN2 = (navigator && navigator.appName 
                       && (navigator.appName == 'Netscape') 
                       && navigator.appVersion 
                       && (parseInt(navigator.appVersion) == 2))?false:true;

        if(hours && not_NN2) { // NN2 cannot handle Dates, so skip this part
            if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
                var numHours = hours;
            } else if (typeof(hours) == 'number') { // calculate Date from number of hours
                var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
            }
        }
        document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
    }
} // WM_setCookie

function WM_readCookie(name) {
    if(document.cookie == '') { // there's no cookie, so go no further
        return false; 
    } else { // there is a cookie
        var firstChar, lastChar;
        var theBigCookie = document.cookie;
        firstChar = theBigCookie.indexOf(name); // find the start of 'name'
        var NN2Hack = firstChar + name.length;
        if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
            firstChar += name.length + 1; // skip 'name' and '='
            lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
            if(lastChar == -1) lastChar = theBigCookie.length;
            return unescape(theBigCookie.substring(firstChar, lastChar));
        } else { // If there was no cookie of that name, return false.
            return false;
        }
    }   
} // WM_readCookie

function WM_killCookie(name, path, domain) {
  var theValue = WM_readCookie(name); // We need the value to kill the cookie
  if(theValue) {
      document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
  }
} // WM_killCookie

if (WM_readCookie("surveyCookie") == "done")
{
    alert("you've had your chance!");
    window.location = "http://book_of_javascript.tripod.com/";
} else {
  
  WM_setCookie("surveyCookie", "done", 240000, "/");
}
// -->
</script>

<h1>Answer me these questions three!</h1>
<form>
What is your name: <input type="text" size="40"><br>
What is your quest: <input type="text" size="40"><br>
What is the average air-speed velocity of an unladen swallow: <input type="text" size="40"><br>
<p>
<input type="button" value="answer" onClick="alert('aaaiiieeeeee!!!!!nthanks for filling out the surveynif you haven't seen Monty Python and Holy Grail, you should!'); window.location='http://book_of_javascript.tripod.com';">
</form>




No Reviews to show


Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.


Subject:

Reviewed By:

Write a review:





Terms of Conditions
Powered By
Avian Hosting
© 2005 snippetlibrary.com All Rights Reserved.