Home Backend Development PHP Tutorial Example code for adding, obtaining and deleting cookies in js

Example code for adding, obtaining and deleting cookies in js

Jul 10, 2017 am 11:24 AM
cookie ie javascript

The application of

cookie is very common in web pages. In this article, I will introduce how to add, get the value, and delete cookies in js. Friends who are interested should not miss it

The code is as follows:

function setCookie(name,value,time){ 
var oDate = new Date(); 
oDate.setDate(oDate.getDate()+time); 
document.cookie = name+"="+value+";expires="+oDate; 
} 


function getCookie(name){ 
var arr = document.cookie.split("; "); 
for(var i=0; i<arr.length; i++){ 
var arr2 = arr[i].split("="); 
if(arr2[0] == name){ 
return arr2[1]; 
} 
} 
return ""; 
} 


function removeCookie(name){ 
setCookie(name,"",0) 
}
Copy after login

JavaScript is a script that runs on the client, so it is generally not possible to set Session because Session runs on the server Endless. Cookies run on the client, so you can use JS to set cookies. Let's analyze the case

//已经验证过
// JavaScript Document
//使用说明: 
//设置缓存:setCookie("name",value);
//获取缓存:var name=getCookie("name");
//删除缓存:delCookie("name");
///设置cookie
function setCookie(NameOfCookie, value, expiredays)
{
 //@参数:三个变量用来设置新的cookie:
 //cookie的名称,存储的Cookie值,
 // 以及Cookie过期的时间.
 // 这几行是把天数转换为合法的日期
 var ExpireDate = new Date ();
 ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
 // 下面这行是用来存储cookie的,只需简单的为"document.cookie"赋值即可.
 // 注意日期通过toGMTstring()函数被转换成了GMT时间。
 document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}
///获取cookie值
function getCookie(NameOfCookie)
{
 // 首先我们检查下cookie是否存在.
 // 如果不存在则document.cookie的长度为0
 if (document.cookie.length > 0)
 {
  // 接着我们检查下cookie的名字是否存在于document.cookie
  // 因为不止一个cookie值存储,所以即使document.cookie的长度不为0也不能保证我们想要的名字的cookie存在
  //所以我们需要这一步看看是否有我们想要的cookie
  //如果begin的变量值得到的是-1那么说明不存在
  begin = document.cookie.indexOf(NameOfCookie+"=");
  if (begin != -1)   
  {
   // 说明存在我们的cookie.
   begin += NameOfCookie.length+1;//cookie值的初始位置
   end = document.cookie.indexOf(";", begin);//结束位置
   if (end == -1) end = document.cookie.length;//没有;则end为字符串结束位置
   return unescape(document.cookie.substring(begin, end));
  }
 }
 return null;
 // cookie不存在返回null
}
///删除cookie
function delCookie (NameOfCookie)
{
 // 该函数检查下cookie是否设置,如果设置了则将过期时间调到过去的时间;
 //剩下就交给操作系统适当时间清理cookie啦
 if (getCookie(NameOfCookie))
 {
  document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
 }
}
Copy after login

The above is the detailed content of Example code for adding, obtaining and deleting cookies in js. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Where are the cookies on your computer? Where are the cookies on your computer? Dec 22, 2023 pm 03:46 PM

Where are the cookies on your computer?

Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

Where are cookies stored?

Where are the mobile cookies? Where are the mobile cookies? Dec 22, 2023 pm 03:40 PM

Where are the mobile cookies?

What should I do if win11 cannot use ie11 browser? (win11 cannot use IE browser) What should I do if win11 cannot use ie11 browser? (win11 cannot use IE browser) Feb 10, 2024 am 10:30 AM

What should I do if win11 cannot use ie11 browser? (win11 cannot use IE browser)

Detailed explanation of where browser cookies are stored Detailed explanation of where browser cookies are stored Jan 19, 2024 am 09:15 AM

Detailed explanation of where browser cookies are stored

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

How to cancel the automatic jump to Edge when opening IE in Win10_Solution to the automatic jump of IE browser page How to cancel the automatic jump to Edge when opening IE in Win10_Solution to the automatic jump of IE browser page Mar 20, 2024 pm 09:21 PM

How to cancel the automatic jump to Edge when opening IE in Win10_Solution to the automatic jump of IE browser page

Frequently Asked Questions and Solutions about Cookie Settings Frequently Asked Questions and Solutions about Cookie Settings Jan 19, 2024 am 09:08 AM

Frequently Asked Questions and Solutions about Cookie Settings

See all articles