Home > Article > Web Front-end > Detailed explanation of how to use javascript to encapsulate cookie instances
Before using cookies, they were all operated in the form of document.cookie. Although the compatibility was good, it was troublesome. I personally like to make wheels, so I encapsulated a tool class for cookies. For a long time, I have liked writing code, but I don't really like text summaries, nor do I like writing fragmentary things. It seems that I have to change it.
Ideas
(1) How to encapsulate and what to encapsulate
How to encapsulate: Use native js to encapsulate it into a tool, so that it can be used anywhere use. Encapsulating document.cookie is the best way, and all operations are based on document.cookie.
How to encapsulate it: it can exist in the form of an object, and at the same time, it can be implemented using getter & setter methods.
(2) Which methods are encapsulated?
get(), set(name, value, opts), remove(name), clear(), getCookies(), etc. I personally feel that encapsulation is There are many ways to use cookies.
Action
(1) Understand cookies. The essence of cookies is HTTP cookies. The object displayed on the client is document.cookie. For more information, you can read my code below and comment
(2) Code: These codes should be very intuitive and can be compressed together with the project code. I think the opening comment below is the key point.
/* * HTTP Cookie:存储会话信息 * 名称和值传送时必须是经过RUL编码的 * cookie绑定在指定的域名下,非本域无法共享cookie,但是可以是在主站共享cookie给子站 * cookie有一些限制:比如IE6 & IE6- 限定在20个;IE7 50个;Opear 30个...所以一般会根据【必须】需求才设定cookie * cookie的名称不分大小写;同时建议将cookie URL编码;路径是区分cookie在不同情况下传递的好方式;带安全标志cookie * 在SSL情况下发送到服务器端,http则不会。建议针对cookie设置expires、domain、 path;每个cookie小于4KB * */ //对cookie的封装,采取getter、setter方式 (function(global){ //获取cookie对象,以对象表示 function getCookiesObj(){ var cookies = {}; if(document.cookie){ var objs = document.cookie.split('; '); for(var i in objs){ var index = objs[i].indexOf('='), name = objs[i].substr(0, index), value = objs[i].substr(index + 1, objs[i].length); cookies[name] = value; } } return cookies; } //设置cookie function set(name, value, opts){ //opts maxAge, path, domain, secure if(name && value){ var cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value); //可选参数 if(opts){ if(opts.maxAge){ cookie += '; max-age=' + opts.maxAge; } if(opts.path){ cookie += '; path=' + opts.path; } if(opts.domain){ cookie += '; domain=' + opts.domain; } if(opts.secure){ cookie += '; secure'; } } document.cookie = cookie; return cookie; }else{ return ''; } } //获取cookie function get(name){ return decodeURIComponent(getCookiesObj()[name]) || null; } //清除某个cookie function remove(name){ if(getCookiesObj()[name]){ document.cookie = name + '=; max-age=0'; } } //清除所有cookie function clear(){ var cookies = getCookiesObj(); for(var key in cookies){ document.cookie = key + '=; max-age=0'; } } //获取所有cookies function getCookies(name){ return getCookiesObj(); } //解决冲突 function noConflict(name){ if(name && typeof name === 'string'){ if(name && window['cookie']){ window[name] = window['cookie']; delete window['cookie']; return window[name]; } }else{ return window['cookie']; delete window['cookie']; } } global['cookie'] = { 'getCookies': getCookies, 'set': set, 'get': get, 'remove': remove, 'clear': clear, 'noConflict': noConflict }; })(window);
(3)example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>cookie example</title> </head> <body> <script type="text/javascript" src="cookie.js" ></script> <script type="text/javascript"> console.log('----------cookie对象-------------'); console.log(cookie); console.log('----------对象-------------'); console.log(cookie.getCookies()); console.log('----------设置cookie-------------'); console.log(cookie.set('name', 'wlh')); console.log('----------设置cookie 123-------------'); console.log(cookie.set('name', 'wlh123')); console.log('----------设置cookie age-------------'); console.log(cookie.set('age', 20)); console.log('----------获取cookie-------------'); console.log(cookie.get('name')); console.log('----------获取所有-------------'); console.log(cookie.getCookies()); console.log('----------清除age-------------'); console.log(cookie.remove('age')); console.log('----------获取所有-------------'); console.log(cookie.getCookies()); console.log('----------清除所有-------------'); console.log(cookie.clear()); console.log('----------获取所有-------------'); console.log(cookie.getCookies()); console.log('----------解决冲突-------------'); var $Cookie = cookie.noConflict(true /*a new name of cookie*/); console.log($Cookie); console.log('----------使用新的命名-------------'); console.log($Cookie.getCookies()); </script> </body> </html>
The above is the detailed content of Detailed explanation of how to use javascript to encapsulate cookie instances. For more information, please follow other related articles on the PHP Chinese website!