Skip to content

Instantly share code, notes, and snippets.

@mjtamlyn
Created May 25, 2012 11:19
Show Gist options
  • Select an option

  • Save mjtamlyn/2787409 to your computer and use it in GitHub Desktop.

Select an option

Save mjtamlyn/2787409 to your computer and use it in GitHub Desktop.
Testing storage options
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
function setCookie(c_name,value,exdays) {
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
$(function () {
// Cookies
$('#cookie-read').html(getCookie('cookie'));
var cookieVal = 'cookie';
setCookie('cookie', cookieVal, 365);
$('#cookie-save').html(cookieVal);
// Session storage
$('#session-read').html(sessionStorage.getItem('session'));
var sessionVal = 'session';
sessionStorage.setItem('session', sessionVal);
$('#session-save').html(sessionVal);
// Local storage
$('#local-read').html(localStorage.getItem('local'));
var localVal = 'local';
localStorage.setItem('local', localVal);
$('#local-save').html(localVal);
})
</script>
</head>
<body>
<h1>Storage Test</h1>
<h2>Cookies</h2>
<p>Read value: <span id="cookie-read"></span></p>
<p>Save value: <span id="cookie-save"></span></p>
<h2>Session Storage</h2>
<p>Read value: <span id="session-read"></span></p>
<p>Save value: <span id="session-save"></span></p>
<h2>Local Storage</h2>
<p>Read value: <span id="local-read"></span></p>
<p>Save value: <span id="local-save"></span></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment