Examples of jquery. cookie. js usage,

Source: Internet
Author: User
Tags set cookie

Examples of jquery. cookie. js usage,

This document describes how to use jquery. cookie. js. We will share this with you for your reference. The details are as follows:

The operation of cookies is always with us when we access a website, recording every action we make and saving information that does not harm user privacy, in this way, the user does not need to repeat the steps again, which greatly facilitates the customer and increases the customer's return rate to the website.

Jquery. cookie. js provides a very simple method to operate cookies in jquery.

$. Cookie (the _ cookie); // get cookie $. cookie (the _ cookie, the _ value); // set cookie $. cookie ('the _ cookies', 'the _ value', {expires: 7}); // set a time-based cookie $. cookie ('the _ cookies', '', {expires:-1}); // Delete $. cookie (the _ cookie, null); // Delete cookie $. cookie (the _ cookie, the _ value, {expires: 7, path: '/', domain: 'jquery. com ', secure: true}); // create a cookie, including the Domain Name of the validity period path.

By default, the expiration time of this plug-in is calculated based on the number of days. We can modify the expiration time by millisecond. 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));}

The following is a simple example: we need to read statistics on a page, but in a period of time (for example, 5 minutes ), the same person can only perform this operation once no matter how many times the page is refreshed. At this time, we can use cookies to implement:

<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 "> // page type that identifies a set of pages var pageType = 20110420; // page id, identifies the unique page 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) ;}) // initialize the data. The one-minute access to the same cookie is counted as one function init_count (pageType, id) {if ($. cookie (the _ cookie + id) {// alert ("cookie already exists"); getViewData (pageType, id );} else {// var cookie expired in 1 minute =$. 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 );}} // calculate the access volume function getViewData (pageType, id) {$. ajax ({type: "get", // use the get method to access the background dataType: "jsonp", // return json-format data jsonp: "callback", url: "/manage. php ", // The background address to be accessed data: {" opp ":" view "," pageType ": pageType," id ": id}, async: false, success: function (data) {// alert (data. total); ('your pc_1'your .html (data. total); ('{pcm_1'{.html (data. record) ;}} // insert or update the page statistics function insert_page (pageType, id) {var j = null; $. ajax ({type: "get", // use the get method to access the background dataType: "jsonp", // return json-format data jsonp: "callback", url: "/manage. php ", // The background address to be accessed 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); ('your pc_1'your .html (data. total); ('{pcm_1'{.html (data. record) ;}}}</script>

I hope this article will help you with jQuery programming.

Articles you may be interested in:
  • How to record user names using cookies in js
  • JS encapsulate cookie operation function instances (set, read, and delete)
  • JavaScript allows you to write, read, and delete cookies.
  • JS uses cookies to remember the anti-Refresh navigation effect of the current location
  • Javascript encapsulate Cookie application interface
  • Read/write operations on cookies using JavaScript
  • Cookie in javascript
  • How to Set, retrieve, and delete cookies in javascript
  • How to Implement the suspended shopping cart using js + cookies
  • How to save browsing records using js operation cookies

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.