❶ js 或者java 實現清除所有Cookies內容的操作,如何實現,是所有的Cookies內容,謝謝
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
}
if(cookies.length > 0)
{
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
var domain = location.host.substr(location.host.indexOf('.'));
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=" + domain;
}
}
js實現
❷ 用java創建的cookie,在頁面上用js怎麼可以獲取呀!
// 函數名稱: getCookie
// 函數功能: 讀取cookie函數
// 入口參數: Name:cookie名稱
function getCookie(Name) {
var search = Name + "=" ;
if(document.cookie.length > 0) {
offset = document.cookie.indexOf(search) ;
if(offset > -1) {
offset += search.length ;
end = document.cookie.indexOf(";", offset)
if(end > -1) {
end = document.cookie.length ;
}
return unescape(document.cookie.substring(offset, end)) ;
} else {
return "" ;
}
}
}
❸ 怎麼在js中取java變數的值,朋友們幫忙說說
Java的變數值,理論上在js中是不能獲取,但是可以通過間接的方式來獲取。提供兩種思路供參考:1、把java變數的值存在cookie中,js通過cookie去取; <pre t="code" l="js">function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}
var value= getCookie("cookie的name名");2、把java變數的值賦值給頁面中<input type="hidden"/>的value,通過js去取;<pre t="code" l="js">var value=document.getElementById("hidden的ID").value;