Skip to content

Instantly share code, notes, and snippets.

@trico
Created June 21, 2011 23:10
Show Gist options
  • Save trico/1039199 to your computer and use it in GitHub Desktop.
Save trico/1039199 to your computer and use it in GitHub Desktop.
Pruebas con localStorage
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<body>
<a href="javascript:void(0)" id="clear">Clear Localstorage</a>
<div id="holas" data-store="holas" class="storeit">
<?php if(!$_COOKIE['holas']){?>
<ul>
<?php for ($i = 1; $i <= 10000; $i++) {?>
<li><?php echo $i?></li>
<?php }?>
</ul>
<?php }?>
</div>
<script>
(function(){
var elements = $('.storeit'),
leng = elements.length;
function setlocalStorage() {
for (i = 0; i<=leng ; i++) {
var elementData = $(elements[i]).data('store');
if(!getCookie(elementData)){
localStorage.setItem(elementData, $(elements[i]).html());
document.cookie = elementData + '=1';
}
}
}
function getlocalStorage() {
for (i = 0; i<=leng ; i++) {
var elementData = $(elements[i]).data('store');
if(getCookie(elementData)){
$(elements[i]).prepend('localstorage:');
$(elements[i]).append(localStorage.getItem(elementData));
}
}
}
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 delCookie(name) {
document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}
function clear() {
for (i = 0; i<=leng ; i++) {
delCookie($(elements[i]).data('store'));
}
localStorage.clear();
}
getlocalStorage();
setlocalStorage();
/*************/
$('#clear').live('click', function(){
clear();
});
})();
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment