jQuery外掛程式,jquery外掛程式庫
Cookie是網站設計者放置在用戶端的小文字檔。Cookie能為使用者提供很多的使得,例如購物網站儲存使用者曾經瀏覽過的產品列表,或者門戶網站記住使用者喜歡選擇瀏覽哪類新聞。 在使用者允許的情況下,還可以儲存使用者的登入資訊,使得使用者在訪問網站時不必每次都鍵入這些資訊
使用方法:
1.引入jquery.cookie.js
2.將cookie寫入檔案
[javascript] view plaincopyprint?
- var COOKIE_NAME = 'username';
- if( $.cookie(COOKIE_NAME) ){
- $("#username").val( $.cookie(COOKIE_NAME) );
- }
- $("#check").click(function(){
- if(this.checked){
- $.cookie(COOKIE_NAME, $("#username").val() , { path: '/', expires: 10 });
- //var date = new Date();
- //date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000)); //三天后的這個時候到期
- //$.cookie(COOKIE_NAME, $("#username").val(), { path: '/', expires: date });
- }else{
- $.cookie(COOKIE_NAME, null, { path: '/' }); //刪除cookie
- }
- });
參數設定:
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.
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。