1

我有一个用于创建、保存和删除 cookie 的 JS 文档,但是它不会在 Chrome 中删除它们?我在这里查看了其他问题,但找不到解决方案。另外,我注释掉的删除cookies功能不起作用,所以我使用了拆分,我不知道为什么!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
 {
 return unescape(y);
 }
 }
 }

 function setCookie(c_name,value,exdays)
 {
 var exdate=new Date();
 exdate.setDate(exdate.getDate() + exdays);
 var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
 document.cookie=c_name + "=" + c_value;
 }



function checkCookie()
{
 var username=getCookie("username");
 var password=getCookie("password");

 if (username!=null && username!="" && password!=null && password!="")
 {
  document.getElementById("username").value=username;
  document.getElementById("password").value=password;
  }
  }

function storeValues()  
{
var username= document.getElementById("username").value;
var password= document.getElementById("password").value;


   setCookie("username",username,365);
 setCookie("password",password,365);

 alert("Cookies stored!")

 }

 //function deleteCookies()

//{
 //setCookie("username", "",-1);
 //setCookie("password", "",-1);

 //alert("Deleted!");
 //}    

 function deleteallcookies()  
 {  
var cookie = document.cookie;  
var arr = cookie.split("; ");  
var i;  
for(i=0;i<arr.length;i++)  
{  
   temp = arr[i].split("=");  
   setCookie(temp[0]);  
 }  


  checkCookie();

  alert("Deleted");  
} 

function show()
{
 var username= document.getElementById("username").value;
 var password= document.getElementById("password").value;

 alert("username:" + username  + "\npassword:" + password);

}


 </script>
 </head>

 <body onload="checkCookie()">
 <form>Name: 
 <input name="Name" type="text" id="username" /><p>
  Password: 
 <input name="Name" type="text" id="password" />
  <p>  
  <p>
  </form>
 <input name="Submit" value="submit" type="button" onclick="storeValues();"/>
 <input name="Show" value="show" type="button" onclick="show();"/>
 <input name="Delete" value="delete" type="button" onclick="deleteallcookies();"/>
 </body>
 </html>
4

0 回答 0