简体   繁体   English

如何用js操作cookie

[英]how to manipulate cookie with js

here this code just stores te options and atlast it produces the result from adio buttons. 在这里,此代码仅存储te选项,最后它通过adio按钮产生结果。 i want it inted of radio button use of textbox or input field. 我希望它使用文本框或输入字段的单选按钮。 so what should be changed? 所以应该改变什么?

<script language="JavaScript">
var timer,timecounter=0
function readCookie(name)
{
   var result=null;
   var myCookie=document.cookie+";";
   var searchName=name+"=";
   var start=myCookie.indexOf(searchName);
   var end;
   if(start!=-1)
    {
      start+=searchName.length;
      end=myCookie.indexOf(";",start);
      result=myCookie.substring(start,end);
    }
  return result;      
}
function writeCookie(name)
{
  var expDate=new Date();
  var day=1*24*60*60;
  expDate.setTime(expDate.getTime()+day);
  document.cookie=name+"="+timecounter;
}
function starttimer(counter,ans)
{
var cook 
    timecounter=parseInt(readCookie(<%=chr(34) & examname&"totaltime"& chr(34)%> ))
   var index=-1
   if(ans=="A")
        index=0
   else if(ans=="B")
        index=1      
   else if(ans=="C")
        index=2      
   else if(ans=="D")
        index=3

   if(document.examform.qno.value-1<=counter&&index!=-1)
        document.examform.elements[index].checked=true
      timer=setInterval("change()",1000)

}
function stoptimer()
{
    writeCookie("<%=examname&"totaltime"%>");  
    clearInterval(timer)
}
function change()
{
    var hrs,mins,secs;
    hrs=parseInt(timecounter/3600);
    mins=parseInt(timecounter/60);
    secs=timecounter%60;
    window.status="Time Remaining : "+hrs+":"+mins+":"+secs;
    rem_time.innerText="Time Remaining :"+hrs+":"+mins+":"+secs;
    timecounter--
    if(timecounter==-1)
      {
        store_ans()
        stoptimer()
        document.examform.submit()    

      }           
}
function store_ans()
{
     var elems=document.examform.elements 
     var ans=""
    for(var i=0;i<elems.length;i++)
     if((elems[i].type=="radio"||elems[i].type=="checkbox")&&elems[i].checked)
         ans=ans+elems[i].value    
    document.examform.answer.value=ans
    if(ans=="")
      document.examform.answer.value="Not Attempted"    
    stoptimer()
}       
</script>

document.cookie : get and set the cookies associated with the current document (Mozilla Developer Network) document.cookie获取并设置与当前文档关联的cookie (Mozilla开发人员网络)

You can manipulate cookies with vanilla JavaScript using the document.cookie global. 您可以使用document.cookie全局使用香草JavaScript处理cookie。

To store multiple cookies, you have to assign document.cookie multiple times, eg to store var a = '1'; var b = '2' 要存储多个cookie,必须多次分配document.cookie ,例如,存储var a = '1'; var b = '2' var a = '1'; var b = '2' you do var a = '1'; var b = '2'

document.cookie = "a=1";
document.cookie = "b=2";

To read the cookie string, simply reference document.cookie , eg console.log(document.cookie) 要读取cookie字符串,只需引用document.cookie ,例如console.log(document.cookie)

Also, your post has several editing and grammatical mistakes. 另外,您的帖子有一些编辑和语法错误。 Additionally, this is a question which can be easily answered with a Google search. 此外,这个问题可以通过Google搜索轻松回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM