Sử dụng cookie trong javascript 2007-07-19 19:07:57

Tác dụng: lưu lại các giá trị bằng javascript, giống như form search hay form Tác giả của phpbasic.com
<script>
function BASIC_GetCookie(Name){
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return ""
}

function BASIC_SetCookie(name, value, days){
if (typeof days!="undefined"){ //if set persistent cookie
var expireDate = new Date()
var expstring=expireDate.setDate(expireDate.getDate()+days)
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()
}
else //else if this is a session only cookie
document.cookie = name+"="+value
}
</script>
Trong đó:
- BASIC_SetCookie(name, value, days) lưu giá trị value vào biến cookie name trong thời gian days
- BASIC_GetCookie(Name): lấy giá trị từ biến cookie Name

Tra loi 12 comment(s) TG 2007-07-19 19:07:57

mapumeo 2007-08-01 04:42:24

Làm sao lấy Cookie SET cho thẻ <input> khi load form (chứa thẻ input> bằng Ajax????

Tra loi

TG 2007-08-01 06:00:35

đặt giá trị của cookie nằm trong chuỗi javascript
<script>
BASIC_SetCookie('cookie_name', 'cookie_value')
var input = '<input type="text" name="demo" value="'+BASIC_GetCookie('cookie_name')+'"';
document.write(input);
</script>

Tra loi

mapumeo 2007-08-02 12:25:24

Nói rõ chút cui, đã load bằng Ajax thì làm sao chạy được <script> chứ lị....

Tra loi

TG 2007-08-02 01:23:19

Vậy thì bạn phải sử dụng hàm $(url,id,eval_str) mà TG đã post và cách dùng như sau:
$("file_noidung","id_hienthi", "document.form_name.input_name.value="+BASIC_GetCookie('cookie_name'));

trong đó:
- file_noidung: file được load bằng AJAX(file này chứ thẻ input
- id_hienthi: id hiển thị nội dung file file_noidung
- form_name,input_name: tên của tag: form,input chưa trong file file_noidung

Tra loi

mapumeo 2007-08-02 01:26:35

Còn cách khác không?
Sao xử dụng cookie của php được vậy.
Rõ ràng đã khai báo $_COOKIE['cookie_name']='cookie_value';
Nhưng khi chạy qua file Ajax thì chưa khởi tạo biến $_COOKIE['cookie_name']???

Tra loi

TG 2007-08-02 01:42:21

hixhix, đang sử dụng cookie trong javascript, tự dưng cái dùng $_COOKIE['cookie'], cái này trong PHP chứ đâu phải trong javascript
** javascript không đọc được cookie do PHP tạo

Tra loi

mapumeo 2007-08-03 12:36:50


Ý tui là tại sao không dùng COOKIE của php cho dễ.
<input value="<?php echo $_COOKIE['cookie_name'];?>" >
Hiểu hông, dùng Javascript bị khó khăn khi load bằng ajax. Nhưng ý tui chính là nó không hiểu biến COOKIE đã khai báo ở file index.php khi chạy qua file ajax.php, TG hiểu không chài.

Tra loi

TG 2007-08-03 02:27:19

bây giờ thì hiểu :D, dùng cookie của js thì server đỡ xử lý hơn :)

Tra loi

Co vịt bầu 2007-08-06 07:54:56

Dùng cái hàm cookie này set giá trị hình như bị lổi font tiếng Việt TG, có fix được không?

Tra loi

TG 2007-08-06 08:52:37

TG cũng bị vậy nhưng không fix được

Tra loi

cocbay 2007-08-06 08:58:13

Xem kỹ thì cocbay fix được. Sử dụng thêm hàm decodeURIComponent() và encodeURIComponent()
Fix lại hàm trên như sau:
function BASIC_GetCookie(Name){
var re=new RegExp(Name+"=[^;]+", "i");
if (document.cookie.match(re))
return decodeURIComponent(document.cookie.match(re)[0].split("=")[1])
return ""
}

function BASIC_SetCookie(name, value, days){
if (typeof days!="undefined"){
var expireDate = new Date()
var expstring=expireDate.setDate(expireDate.getDate()+days)
document.cookie = name+"="+encodeURIComponent(value)+"; expires="+expireDate.toGMTString()
}
else
document.cookie = name+"="+encodeURIComponent(value)
}

Tra loi

mapumeo 2007-08-06 09:09:06

oki, fix được rồi, cảm ơn cocbay nhe.

Tra loi

Y kien