The examples in this article describe the usage of jquery.cookie.js. Share it with everyone for your reference, the details are as follows:
The operation of cookies will accompany us all the time when we visit a website, recording our every move, and information that does not harm the user's privacy will be saved, so that the user does not have to repeat the operation again. Steps, this greatly facilitates customers and increases their return rate to the website.
jquery.cookie.js provides a very simple way to operate cookies in jquery.
$.cookie('the_cookie'); // 获得cookie $.cookie('the_cookie', 'the_value'); // 设置cookie $.cookie('the_cookie', 'the_value', { expires: 7 }); //设置带时间的cookie $.cookie('the_cookie', '', { expires: -1 }); // 删除 $.cookie('the_cookie', null); // 删除 cookie $.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});//新建一个cookie 包括有效期 路径 域名等
The default expiration of this plug-in is calculated in days. We can modify it to calculate it in milliseconds. The modification is as follows:
if (typeof options.expires === 'number') { //var days = options.expires, t = options.expires = new Date(); //t.setDate(t.getDate() + days); var seconds = options.expires, t = options.expires = new Date(); t.setTime(t.getTime() + seconds); //t.setTime(t.getTime() + days); //date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000)); }
Here is a simple example: We need to perform reading statistics on a certain page, but within a period of time (such as 5 minutes), no matter how many times the same person refreshes the page, it can only be counted once. . This can be achieved with the help of cookies:
<script language="javascript" src="/js/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="/js/jquery.cookie.js"></script> <script language="javascript" src="/js/jquery.jsonp-2.1.4.min.js"></script> <script type="text/javascript"> // 页面类型,标识一组页面 var pageType = 20110420; // 页面id,标识唯一一个页面 var url = window.location.href; var url_arr = url.split("."); var id = url_arr[url_arr.length - 2]; //var id = 2; //var cookie = $.cookie('the_cookie'+id, true, { expires: 5/24/60/60 }); $(document).ready(function(){ init_count(pageType, id); }) // 初始化数据,同一个cookie一分钟的访问量都算一次 function init_count(pageType, id){ if($.cookie('the_cookie'+id)){ //alert("cookie已存在"); getViewData(pageType, id); } else { // 1分钟过期 var cookie = $.cookie('the_cookie'+id, 'Gonn', { expires: 1000 * 60 * 5 }); //$.cookie('the_cookie'+id, 'Gonn'); //var cookie = $.cookie('the_cookie'+id); //alert(cookie); insert_page(pageType, id); } } // 不插入与更新时统计访问量 function getViewData(pageType, id){ $.ajax({ type: "get", //使用get方法访问后台 dataType: "jsonp", //返回json格式的数据 jsonp:"callback", url: "/manage.php", //要访问的后台地址 data:{"opp":"view", "pageType":pageType, "id":id}, async: false, success: function(data){ //alert(data.total); $('#pc_1').html(data.total); $('#pcm_1').html(data.record); } }) } // 插入或者更新页面统计 function insert_page(pageType, id){ var j = null; $.ajax({ type: "get", //使用get方法访问后台 dataType: "jsonp", //返回json格式的数据 jsonp:"callback", url: "/manage.php", //要访问的后台地址 data:{"opp":"insert", "pageType":pageType, "id":id}, async: false, success: function(data){ //alert(msg.current); //alert(msg.record); j = data; //alert("111"); //alert(j.total); $('#pc_1').html(data.total); $('#pcm_1').html(data.record); } }) } </script>
I hope this article will be helpful to everyone in jQuery programming.