jQuery外掛程式,jquery外掛程式庫

來源:互聯網
上載者:User

jQuery外掛程式,jquery外掛程式庫
Cookie是網站設計者放置在用戶端的小文字檔。Cookie能為使用者提供很多的使得,例如購物網站儲存使用者曾經瀏覽過的產品列表,或者門戶網站記住使用者喜歡選擇瀏覽哪類新聞。 在使用者允許的情況下,還可以儲存使用者的登入資訊,使得使用者在訪問網站時不必每次都鍵入這些資訊

使用方法:

1.引入jquery.cookie.js

2.將cookie寫入檔案[javascript] view plaincopyprint?
  1.     var COOKIE_NAME = 'username';  
  2.     if( $.cookie(COOKIE_NAME) ){  
  3.         $("#username").val(  $.cookie(COOKIE_NAME) );  
  4.     }  
  5.     $("#check").click(function(){  
  6.         if(this.checked){  
  7.             $.cookie(COOKIE_NAME, $("#username").val() , { path: '/', expires: 10 });  
  8.             //var date = new Date();  
  9.             //date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000)); //三天后的這個時候到期  
  10.             //$.cookie(COOKIE_NAME, $("#username").val(), { path: '/', expires: date });  
  11.         }else{  
  12.             $.cookie(COOKIE_NAME, null, { path: '/' });  //刪除cookie  
  13.         }  
  14.     });  
參數設定:
expires: (Number | Date)      有效期間,可以設定一個整數作為有效期間(單位:天),也可以設定一個日期對象作為Cookie的到期日期。如果指定日期為負數,那麼此cookie將被刪除;如果不設定或者設定為null,那麼此cookie將被當作Session Cookie處理,並且在瀏覽器關閉後刪除

path:  (String)   Cookie的路徑屬性,預設是建立該cookie的頁面路徑

domain: (String)   Cookie的網域名稱屬性,預設是建立該cookie的頁面網域名稱

secure: (Boolean)  如果設為true,那麼此cookie的傳輸會要求一個安全性通訊協定,例如HTTPS

Usage

Create session cookie:

$.cookie('the_cookie', 'the_value');

Create expiring cookie, 7 days from then:

$.cookie('the_cookie', 'the_value', { expires: 7 });

Create expiring cookie, valid across entire site:

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });

Read cookie:

$.cookie('the_cookie'); // => "the_value" $.cookie('not_existing'); // => undefined

Read all available cookies:

$.cookie(); // => { "the_cookie": "the_value", "...remaining": "cookies" }

Delete cookie:

// Returns true when cookie was found, false when no cookie was found...$.removeCookie('the_cookie'); 
// Same path as when the cookie was written...$.removeCookie('the_cookie', { path: '/' });

Note: when deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie, unless you're relying on the default options that is.

Configurationraw

By default the cookie value is encoded/decoded when writing/reading, usingencodeURIComponent/decodeURIComponent. Bypass this by setting raw to true:

$.cookie.raw = true;
json

Turn on automatic storage of JSON objects passed as the cookie value. Assumes JSON.stringify andJSON.parse:

$.cookie.json = true;
Cookie Options

Cookie attributes can be set globally by setting properties of the $.cookie.defaults object or individually for each call to $.cookie() by passing a plain object to the options argument. Per-call options override the default options.

expires
expires: 365

Define lifetime of the cookie. Value can be a Number which will be interpreted as days from time of creation or a Date object. If omitted, the cookie becomes a session cookie.

path
path: '/'

Define the path where the cookie is valid. By default the path of the cookie is the path of the page where the cookie was created (standard browser behavior). If you want to make it available for instance across the entire domain use path: '/'. Default: path of page where the cookie was created.

Note regarding Internet Explorer:

Due to an obscure bug in the underlying WinINET InternetGetCookie implementation, IE’s document.cookie will not return a cookie if it was set with a path attribute containing a filename.

(From Internet Explorer Cookie Internals (FAQ))

This means one cannot set a path using path: window.location.pathname in case such pathname contains a filename like so: /check.html (or at least, such cookie cannot be read correctly).

domain
domain: 'example.com'

Define the domain where the cookie is valid. Default: domain of page where the cookie was created.

secure
secure: true

If true, the cookie transmission requires a secure protocol (https). Default: false.

Converters

Provide a conversion function as optional last argument for reading, in order to change the cookie's value to a different representation on the fly.

Example for parsing a value into a number:

$.cookie('foo', '42'); $.cookie('foo', Number); // => 42

Dealing with cookies that have been encoded using escape (3rd party cookies):

$.cookie.raw = true; $.cookie('foo', );

You can pass an arbitrary conversion function.

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.