Home  >  Article  >  Web Front-end  >  Example tutorial on how to implement cookie saving, reading and deletion functions

Example tutorial on how to implement cookie saving, reading and deletion functions

零下一度
零下一度Original
2017-06-24 14:34:511055browse

Suppose there is a situation where you need to enter a piece of data on page A, use a JS variable to save this data, and at the same time refer to page B, use this variable. Due to the limitations of the JS code, the effect of transmitting data cannot be achieved. .

Then we use cookies to store read data.

Cookie is in the format of name=value. Each cookie is usually separated by ";".

Text box input data:

Use JS to set cookie:

 1        window.onload=function(){ 2                 var oTxt=document.getElementById('oTxt'); 3                 oTxt.oninput=function(){ 4                     //这里引用函数 5                     getCookie('name',oTxt.value); 6                 } 7                 function getCookie(name,value,oDay){ 8                     //判断是否有没有oDay,如果没有默认为5天 9                     oDay=oDay||5;10                     var oTime=new Date();11                     oTime.setDate(oTime.getDate()+oDay);12                     document.cookie=name+'='+value+';path=/;expires='+oTime;13                     //存放一条数据14                 }15             }

expires is the saving time:

JS reads the value of the cookie and prints it out:

 1       window.onload=function(){ 2                 var p=document.getElementsByTagName('p');                
 3                 function getCookie(){ 4                     //分隔成数组的形式 得到 ["name", "20170615"] 5                     var data=document.cookie.split('='); 6                     //判断name 7                     if(data[0] == 'name'){ 8                     return data[1]; 9                        }10                 }11                 var oValue=getCookie();12                 document.write(oValue);13             }

value is output to the page.

Each browser contains a limited number of cookies:

Microsoft points out that Internet Explorer 8 increases cookie limits That's 50 per domain, but IE7 also seems to allow 50 cookies per domain.

Firefox has a cookie limit of 50 per domain name.

Opera has a cookie limit of 30 per domain name.

Safari/WebKit seems to have no cookie restrictions. However, if there are many cookies, the header size will exceed the processing limit of the server, causing an error.

  Note: "Each domain name is limited to 20 cookies" will no longer be correct!

Due to the limited content space of cookies, unnecessary ones can be deleted.

JS delete this name:

 function removeCookie(name){
          setCookie(name,'asdas',-1);
      }

The above is the entire content of this article, I hope you all like it.

The above is the detailed content of Example tutorial on how to implement cookie saving, reading and deletion functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn