跳到主要內容

使用jQuery的Plugin管理cookie

為了能在javascript上管理cookie,因此採用了jQuery的Plugin:jQuery.Cookie

如何使用呢?

上面的連結是連到github,可以看到有個README.md檔案,裡面有說明如何使用。
以下是範例內容
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
//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'); // => null

//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: '/' });

這時候就可用PHP印出cookie,確認是否有成功。
1
2
3
<?php
var_dump($_COOKIE);
?>

(其實這篇文章主要是記錄jQuery.Cookie的連結XD)

留言

這個網誌中的熱門文章

input 陣列 - PHP

要如何在 input 裡面回傳陣列資料呢? 就寫了一個簡單的小範例。 以下是 php 原始碼 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?php /** 印出Get裡面的資訊 **/ if ( ! empty ( $_GET )) print_r ( $_GET ); /** 初始資料設定 **/ $id = array ( 0 => '#0001' , 1 => '#0002' ); $data [ $id [ 0 ]] = array ( 'date' => '2012-08-12' , 'note' => '上海出差' ); $data [ $id [ 1 ]] = array ( 'date' => '2012-08-17' , 'note' => '北京出差' ); /** 印出表單資訊 **/ echo "<form method='get'>" ; foreach ( $data as $key => $row ){ echo "<input type='text' name=' { $key } [date]' value=' { $row [ 'date' ] } ' />" ; echo "<input type='text' name=' { $key } [note]' value=' { $row [ 'note' ] } ' />" ; echo "<br />" ; } echo "<input type='submit' value='送出' />" ; e...