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
<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) 2007-07-19 19:07:57
mapumeo 2007-08-01 04:42:24
Tra loi
TG 2007-08-01 06:00:35
<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
Tra loi
TG 2007-08-02 01:23:19
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
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
** 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
Tra loi
Co vịt bầu 2007-08-06 07:54:56
Tra loi
TG 2007-08-06 08:52:37
Tra loi
cocbay 2007-08-06 08:58:13
Fix lại hàm trên như sau:
var re=new RegExp(Name+"=[^;]+", "i");
if (document.cookie.match(re))
return decodeURIComponent(document.cookie.match(re)[0].split("=")[1])
return ""
}
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
Tra loi
Y kien