Instantly share code, notes, and snippets.
-
Save idoushiki/7e92eb84539c8de4b6a6cf15cf5294bb to your computer and use it in GitHub Desktop.
本、参考
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Cookie(name){ | |
this.$name=name; | |
var allcookies=document.cookie; | |
if(allcookies=="")return; | |
var cookies=allcookies.split(';'); | |
var cookie=null; | |
for(var i=0;i<cookies.length;i++){ | |
if(cookies[i].substring(0,name.length+1)==(name+"=")){ | |
cookie=cookies[i]; | |
break; | |
} | |
} | |
if(cookie==null)return; | |
var cookieval=cookie.substring(name.length+1); | |
var a=cookieval.split('&'); | |
for(var i=0;i<a.length;i++) | |
a[i]=a[i].split(':'); | |
for(var i=0;i<a.length;i++){ | |
this[a[i][0]]=decodeURIComponent(a[i][1]); | |
} | |
} | |
Cookie.prototype.store=function(daysToLive,path,domain,secure){ | |
var cookieval=""; | |
for(var prop in this){ | |
if((prop.charAt(0)=='$')||((typeof this[prop])=='function')) | |
continue; | |
if(cookieval !="")cookieval+='&'; | |
cookieval+=prop+':'+encodeURIComponent(this[prop]); | |
} | |
var cookie=this.$name+'='+cookieval; | |
if(daysToLive||daysToLive==0){ | |
cookie+=";max-age="+(daysToLive*24*60*60); | |
} | |
if(path)cookie+=";path="+path; | |
if(domain)cookie+="; domain="+domain; | |
if(secure)cookie+="; secure"; | |
document.cookie=cookie; | |
} | |
Cookie.prototype.remove=function(path,domain,secure){ | |
for(var prop in this){ | |
if(prop.charAt(0)!='$'&&typeof this[prop]!='function') | |
delete this[prop]; | |
} | |
this.store(0,path,domain,secure); | |
} | |
Cookie.enabled=function(){ | |
if(navigator.cookieEnabled!=undefined)return navigator.cookieEnabled; | |
if(Cookie.enabled.cache!=undefined)return Cookie.enabled.cache; | |
document.cookie="testcookie=test;max-age=10000"; | |
var cookies=document.cookie; | |
if(cookies.indexOf("testcookie=test")==-1){ | |
return Cookie.enabled.cache=false; | |
} | |
else{ | |
document.cookie="testcookie=test;max-age=0;" | |
return Cookie.enabled.cache=true; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<script src="Cookie.js"></script> | |
<script> | |
var cookie=new Cookie("vistordata"); | |
if(!cookie.name||!cookie.color){ | |
cookie.name=prompt("what",""); | |
cookie.color=prompt("favorite",""); | |
} | |
if(!cookie.visits)cookie.visits=1; | |
else cookie.visits++; | |
cookie.store(10); | |
document.write('<h1 style="color:'+cookie.color+'">'+ | |
'www'+cookie.name+'!'+'</h1>'+ | |
'<p>aaad'+cookie.visits+'times.'+ | |
'<button onclick="window.cookie.remove();">foooooo</button>'); | |
</script> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<script> | |
document.cookie="version="+document.lastModified+ | |
";max-age="+(60*60*24*365); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment