Skip to content

Instantly share code, notes, and snippets.

@ChrisMoney
Created February 15, 2012 15:15
Show Gist options
  • Select an option

  • Save ChrisMoney/b47bd523e033cb09e2d3 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisMoney/b47bd523e033cb09e2d3 to your computer and use it in GitHub Desktop.
Javascript --Set Cookie
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN*
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language = "JavaScript" type = "text/JavaScript">
//Create function to store cookie
function setCookie(username)
{
var expirationdate=new Date();
expirationdate.setDate(expirationdate.getDate() + expirationdays);
var c_value=escape(value) + ((expirationday==null) ? "" : "; expires="+expirationdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
//Display message from stored cookie
function checkCookie()
{
var username=getCookie("username");
if (username!=null && username!="")
{
alert("Welcome again " + username);
}
else
{
username=prompt("Please enter your name:","");
if (username!=null && username!="")
{
setCookie(username);
}
}
}
//Get Cookie
function getCookie(c_name)
{
var x=document.cookie.(username);
for (i=0;i<x.length;i++)
{
x=document.cookie[i].substr(0,document.cookie[i].indexOf("="));
y=document.cookie[i].substr(document.cookie[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==username)
{
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment