❶ 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;