Combined with JavaScript authoritative guidelines, coupled with the information collected online during the project development, two methods for settings and obtaining cookies have been compiled.
Copy code code as follows:
<script>
// Set the cookie method
Function setCookie (name, value) {
var exp = new date ();
exp.settime (exp.gettime () + 1*60*60*60*1000); //
document.cookie = name + "=" + escape (value) + "; expires =" + exp.togmtring ();
}
/*When accessing cookie, you generally encode the characters that are easy to inject. The corresponding way should be decoded when obtaining cookies. There are many coding methods. If you have time, write a blog about encoding and decoding*/
// Set the cookie method two directly stores cookie
document.cookie = "HomePage = //www.vevb.com";
/*------------------------------------------------ ------------------------------------------------ -----*/
// Take the cookies function method
Function getCookie (name) {
var anrr = document.cookie.match (new regexp ("(" (^|) "+name+" = ([^;]*) (; | $) "))));
if (Arr! = NULL)
Return unescape (ARR [2]);
Return null;
}
// Take the cookies function method 2
Function getCookie (key) {
if (key == null)
Return null;
If (Object.prototype.tostring.call (Key) == '[Object String]' || Object.prototype.tostring.call (key) == '[Object Number]')
{{
var arstr = document.cookie.split (";");
for (var I = 0; I <arrstr.Length; i ++) {
var test = arstr [i] .split ("=");
if (temp [0] == key)
Return unescape (temp [1]);
}
Return null;
}
Return null;
}
</script>
When studying, many JS methods encounter information on the Internet until they are mastered.