当前位置:编程学习 > html/css >>

cookie的读写操作

在一个页面中把内容写入cookie中,在另一个页面中获得并显示在页面中(在safari和firefox上可以但在chrome 上不可以,不知道为什么。)
第一个页面:点击按钮把表单内容写入cookie,
[html] 
<!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>setCookie</title> 
 
<!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>test2</title> 
<script type="text/javascript"> 
 
        //写cookie 
        function writeCookie(name, value, hours){  
        var expire = "";  
        if(hours != null){  
          expire = new Date((new Date()).getTime() + hours * 3600000);  
          expire = "; expires=" + expire.toGMTString();  
        }  
        document.cookie = name + "=" + escape(value) + expire;  
        }  
         
        //获得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 ""; 
            }  
        }  
     //点击如片获得cookie 
    function getName(){ 
            var button=document.getElementById("button"); 
            var inhtml=document.getElementById("inhtml"); 
            button.onclick=function(){ 
                 
                var name=getCookie("username") ; 
                 
                inhtml.innerHTML=name; 
            } 
         
        } 
        //点击提交写入cookie 
    function add(){ 
        var connect=document.getElementById("connect"); 
        alert(connect.value); 
         
        writeCookie("username", connect.value,24); 
        } 
 
</script> 
</head> 
 
<body onload="getName();"> 
 
</body> 
 
 
<form onsubmit="add();"> 
    <input type="text"  id="connect"/> 
    <input type="submit" value="提交" /> 
</form> 
 
<span id="inhtml"></span> 
 
</head> 
 
<body> 
</body> 
</html> 

<!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>setCookie</title>

<!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>test2</title>
<script type="text/javascript">

  //写cookie
  function writeCookie(name, value, hours){
  var expire = "";
  if(hours != null){
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
  }
  
  //获得cookie
  function getCookie(Name)
 

补充:web前端 , HTML/CSS  ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,