Home > Web Front-end > JS Tutorial > body text

A class in javascript that is convenient for adding, deleting and modifying cookies_javascript skills

WBOY
Release: 2016-05-16 17:49:13
Original
1227 people have browsed it

It is mainly assembled by analyzing the document.cookie string.
Review the operation of cookies in javascript:
To add cookies, you can use document.cookie="userId=111";
The full version can be used: document.cookie="userId=111;domain=.google.com;path=;secure=secure;expire=" date.toGMTString();
You can set the cookie expiration time, domain name, path
Required To delete, just set the expire time to before now.
Now add the javascript.cookie.js class I modified

Copy code The code is as follows:

/*
cookie helper class
easy to write,get,delete
*/
var myCookie={
get :function(name){
if(typeof name != "undefined")
{
//if name given call the get value function
return myCookie_get(name);
}else {
//if name is not given,i want get all the cookie item
return myCookie_getAll();
}
},
add:function(name,value,options){
//write the cookie
myCookie_add(name,value,options);
},
delete:function(name){
//delete the cookie
myCookie_add(name, null);
}
}
String.prototype.Trim = function()
{
return this.replace(/^s /g,"").replace(/s $ /g,"");
}
/*
cookie write function
@name:the cookie name not null
@value:the cookie value null==delete the cookie
@option:{"expires":expire time;"path":/;"domain":localhost;"secure":secure}
*/
function myCookie_add(name,value,options)
{
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options .expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime( ) (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' date.toUTCString() ; // use expires attribute, max-age is not supported by IE
}
var path = options.path ? '; path=' options.path : '';
var domain = options.domain ? '; domain=' options.domain : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
}
}
/*
get the name cookie
@name:the cookie's name
*/
function myCookie_get(name)
{
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(' ;');
for (var i = 0; i < cookies.length; i ) {
var cookie = cookies[i].Trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length 1) == (name '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length 1));
break;
}
}
}
return cookieValue;
}
/*
get all the cookie return as a json
*/
function myCookie_getAll()
{
var cookieArray = new Array();
var str="";
var temp;
if (document.cookie && document.cookie != '' ) {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i ) {
var cookie = cookies[i]. Trim();
temp=cookie.split('=');
//take the
cookieArray.push("{"name":"" decodeURIComponent(temp[0]) "", "value":"" decodeURIComponent(temp[1]) ""}");
}
str=cookieArray.join(",");
}
str="[" str "]";
return eval('(' str ')');
}

The call is also quite simple
Copy code The code is as follows:

myCookie.add("useraccount","admin",{"expires":5});//Add a period For 5 days of cookies
alert(myCookie.get("useraccount"));//Get cookies
cookies=myCookie.get();//Get all cookies
for(var i=0 ;i{
alert(cookies[i]["name"] ":" cookies[i]["value"]);
}
myCookie. delete("useraccount");//Delete the cookie just added
alert(myCookie.get("useraccount"));

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!