<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Snipplr</title>
<link>http://snipplr.com/language/javascript/tags/cookie</link>
<description>Recent snippets posted on Snipplr.com</description>
<language>en-us</language>
<pubDate>Tue, 21 May 2013 17:15:52 GMT</pubDate>
<item>
<title>(JavaScript) Get and Set Cookie - devnull69</title>
<link>http://snipplr.com/view/56007/get-and-set-cookie/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Fri, 01 Jul 2011 22:13:57 GMT</pubDate>
<guid>http://snipplr.com/view/56007/get-and-set-cookie/</guid>
</item>
<item>
<title>(JavaScript) Guardar cookies javascript - victorcastelan</title>
<link>http://snipplr.com/view/52347/guardar-cookies-javascript/</link>
<description><![CDATA[ <p>Guardar cookies en javascript. Fácil y rápido</p> ]]></description>
<pubDate>Wed, 20 Apr 2011 04:27:50 GMT</pubDate>
<guid>http://snipplr.com/view/52347/guardar-cookies-javascript/</guid>
</item>
<item>
<title>(JavaScript) Cookie handling functions - pmw57</title>
<link>http://snipplr.com/view/45954/cookie-handling-functions/</link>
<description><![CDATA[ <p>Cleaned up (with the help of jslint.com)  versions of cookie handling functions</p> ]]></description>
<pubDate>Mon, 20 Dec 2010 11:27:30 GMT</pubDate>
<guid>http://snipplr.com/view/45954/cookie-handling-functions/</guid>
</item>
<item>
<title>(JavaScript) jsCookies - my simple easy pure js javascript cookies function - brandonjp</title>
<link>http://snipplr.com/view/36790/jscookies--my-simple-easy-pure-js-javascript-cookies-function/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Wed, 07 Jul 2010 04:01:09 GMT</pubDate>
<guid>http://snipplr.com/view/36790/jscookies--my-simple-easy-pure-js-javascript-cookies-function/</guid>
</item>
<item>
<title>(JavaScript) Advanced use of jQuery cookie plugin to show/hide a notice, includes changing the HTML text of the link - phil7085</title>
<link>http://snipplr.com/view/36373/advanced-use-of-jquery-cookie-plugin-to-showhide-a-notice-includes-changing-the-html-text-of-the-link/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Sat, 26 Jun 2010 23:18:18 GMT</pubDate>
<guid>http://snipplr.com/view/36373/advanced-use-of-jquery-cookie-plugin-to-showhide-a-notice-includes-changing-the-html-text-of-the-link/</guid>
</item>
<item>
<title>(JavaScript) Simple use of jQuery cookie plugin to show/hide a notice - phil7085</title>
<link>http://snipplr.com/view/36372/simple-use-of-jquery-cookie-plugin-to-showhide-a-notice/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Sat, 26 Jun 2010 23:18:17 GMT</pubDate>
<guid>http://snipplr.com/view/36372/simple-use-of-jquery-cookie-plugin-to-showhide-a-notice/</guid>
</item>
<item>
<title>(JavaScript) Simple JavaScript Cookie Functions - Jamie</title>
<link>http://snipplr.com/view/30873/simple-javascript-cookie-functions/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Sun, 04 Apr 2010 13:16:26 GMT</pubDate>
<guid>http://snipplr.com/view/30873/simple-javascript-cookie-functions/</guid>
</item>
<item>
<title>(JavaScript) Cookie - Piotrek290</title>
<link>http://snipplr.com/view/21740/cookie/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Sat, 24 Oct 2009 07:50:51 GMT</pubDate>
<guid>http://snipplr.com/view/21740/cookie/</guid>
</item>
<item>
<title>(JavaScript) LiveJournal XSS Cookie Stealer - abwaters</title>
<link>http://snipplr.com/view/17165/livejournal-xss-cookie-stealer/</link>
<description><![CDATA[ <p>Note that this no longer works</p> ]]></description>
<pubDate>Sat, 18 Jul 2009 20:47:12 GMT</pubDate>
<guid>http://snipplr.com/view/17165/livejournal-xss-cookie-stealer/</guid>
</item>
<item>
<title>(JavaScript) JavaScript Cookie Management Class - MMDeveloper</title>
<link>http://snipplr.com/view/8424/javascript-cookie-management-class/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Tue, 16 Sep 2008 08:42:15 GMT</pubDate>
<guid>http://snipplr.com/view/8424/javascript-cookie-management-class/</guid>
</item>
<item>
<title>(JavaScript) Cookies - wizard04</title>
<link>http://snipplr.com/view/7606/cookies/</link>
<description><![CDATA[ <p>Set, get, and erase cookies.</p> ]]></description>
<pubDate>Wed, 30 Jul 2008 14:45:54 GMT</pubDate>
<guid>http://snipplr.com/view/7606/cookies/</guid>
</item>
<item>
<title>(JavaScript) Pop up once - valcartei</title>
<link>http://snipplr.com/view/3843/pop-up-once/</link>
<description><![CDATA[ <p>displays a popup window only the first time the visitor gets to the website for expDays.
It uses cookie to do that. expDays set the n. of days you want the cookie to live, default set to 1.</p> ]]></description>
<pubDate>Thu, 04 Oct 2007 05:24:04 GMT</pubDate>
<guid>http://snipplr.com/view/3843/pop-up-once/</guid>
</item>
<item>
<title>(JavaScript) Cookie functions in javascript - elightbo</title>
<link>http://snipplr.com/view/2363/cookie-functions-in-javascript/</link>
<description><![CDATA[ <p>createCookie

When calling createCookie() you have to give it three bits of information: the name and value of the cookie and the number of days it is to remain active. In this case the name-value pair should become ppkcookie=testcookie and it should be active for 7 days.

createCookie('ppkcookie','testcookie',7)

If you set the number of days to 0 the cookie is trashed when the user closes the browser. If you set the days to a negative number the cookie is trashed immediately.

The function receives the arguments and starts doing its job.

function createCookie(name,value,days) {

First of all see if there is a days value. If there isn't we don't need to do the time calculation.

	if (days) {

If there is, create a new Date object containing the current date.

		var date = new Date();

Now get the current Time (in milliseconds) and add the required number of days (in milliseconds). Set the Time of the date to this new value, so that it now contains the date in milliseconds that the cookie should expire.

		date.setTime(date.getTime()+(days*24*60*60*1000));

Set the variable expires to this date in the UTC/GMT format required by cookies.

		var expires = "; expires="+date.toGMTString();
	}

If 0 is passed to the function, expires is not set and the cookie expires when the user closes his browser..

	else var expires = "";

Finally write the new cookie into document.cookie in the correct syntax.

	document.cookie = name+"="+value+expires+"; path=/";
}

Cookie created.
readCookie

To read out a cookie, call this function and pass the name of the cookie. Put the name in a variable. First check if this variable has a value (if the cookie does not exist the variable becomes null, which might upset the rest of your function), then do whatever is necessary.

var x = readCookie('ppkcookie1')
if (x) {
	[do something with x]
}

The function receives the argument and starts.

function readCookie(name) {

We're going to search for the name of the cookie, followed by an =. So create this new string and put it in nameEQ:

	var nameEQ = name + "=";

Then split document.cookie on semicolons. ca becomes an array containing all cookies that are set for this domain and path.

	var ca = document.cookie.split(';');

Then we go through the array (so through all cookies):

	for(var i=0;i < ca.length;i++) {

Set c to the cookie to be checked.

		var c = ca[i];

If the first character is a space, remove it by using the substring() method. Continue doing this until the first character is not a space.

		while (c.charAt(0)==' ') c = c.substring(1,c.length);

Now string c begins with the name of the current cookie. If this is the name of the desired cookie

		if (c.indexOf(nameEQ) == 0)

we've found what we were looking for. We now only need to return the value of the cookie, which is the part of c that comes after nameEQ. By returning this value we also end the function: mission accomplished.

		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}

If, after having gone through all cookies, we haven't found the name we're looking for, the cookie is not present. We return null.

	return null;
}

Cookie read.
eraseCookie

Erasing is extremely simple.

eraseCookie('ppkcookie')

Pass the name of the cookie to be erased

function eraseCookie(name) {

and call createCookie() to set the cookie with an expiry date of one day ago.

	createCookie(name,"",-1);
}

The browser, seeing that the expiry date has passed, immediately removes the cookie.</p> ]]></description>
<pubDate>Tue, 20 Mar 2007 07:45:12 GMT</pubDate>
<guid>http://snipplr.com/view/2363/cookie-functions-in-javascript/</guid>
</item>
<item>
<title>(JavaScript) Redirect cookie - markhope</title>
<link>http://snipplr.com/view/608/redirect-cookie/</link>
<description><![CDATA[ <p>redirects a splash page if visited before. Number of days until expiry can be set</p> ]]></description>
<pubDate>Mon, 31 Jul 2006 04:33:34 GMT</pubDate>
<guid>http://snipplr.com/view/608/redirect-cookie/</guid>
</item>
<item>
<title>(JavaScript) Redirect cookie once per session - markhope</title>
<link>http://snipplr.com/view/606/redirect-cookie-once-per-session/</link>
<description><![CDATA[ <p>Redirect a splash page using javascript. This snippet allows one redirect per session making the backwards navigation possible using the back button.</p> ]]></description>
<pubDate>Mon, 31 Jul 2006 04:11:43 GMT</pubDate>
<guid>http://snipplr.com/view/606/redirect-cookie-once-per-session/</guid>
</item>
</channel>
</rss>