Home > Web Front-end > JS Tutorial > Detailed explanation of how JavaScript determines whether to enable cookies and operate cookie codes

Detailed explanation of how JavaScript determines whether to enable cookies and operate cookie codes

伊谢尔伦
Release: 2017-07-25 15:19:29
Original
1767 people have browsed it

Determine whether to enable cookies

<script>
 function checkCookie() {
  var result=false;
  if(navigator.cookiesEnabled){ return true; }
  document.cookie = "testcookie=yes;";
  
  var setCookie = document.cookie;
  
  if (setCookie.indexOf("testcookie=yes") > -1){
   result=true;
  }else{
   document.cookie = "";
  }
  
  return result;
 }
  
  if(!checkCookie()){
  alert("对不起,您的浏览器的Cookie功能被禁用,请开启"); 
  }else{
  alert("Cookie 成功开启");
  }
</script>
Copy after login

Operation cookie

// 1. 设置COOKIE
  
// 简单型
  
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
  
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}
  
// 完整型
function SetCookie(name,value,expires,path,domain,secure)
{
var expDays = expires*24*60*60*1000;
  
var expDate = new Date();
expDate.setTime(expDate.getTime()+expDays);
  
var expString = ((expires==null) ? "" : (";expires=”+expDate.toGMTString()))
var pathString = ((path==null) ? "" : (";path="+path))
var domainString = ((domain==null) ? "" : (";domain="+domain))
var secureString = ((secure==true) ? ";secure" : "" )
document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString;
}
  
  
// 2.获取指定名称的cookie值:
  
function getCookie(c_name)
{
if (document.cookie.length>0)
 {
 c_start=document.cookie.indexOf(c_name + "=")
 if (c_start!=-1)
 {
 c_start=c_start + c_name.length+1
 c_end=document.cookie.indexOf(";",c_start)
 if (c_end==-1) c_end=document.cookie.length
 return unescape(document.cookie.substring(c_start,c_end))
 }
 }
return ""
}
  
  
// 3.删除指定名称的cookie:
  
function ClearCookie(name)
{
var expDate = new Date();
expDate.setTime(expDate.getTime()-100);
  
document.cookie=name+”=;expires=”+expDate.toGMTString();
  
}
  
// 4. 检测cookie:
  
function checkCookie()
{
username=getCookie(&#39;username&#39;)
if (username!=null && username!="")
 {alert(&#39;Welcome again &#39;+username+&#39;!&#39;)}
else
 {
 username=prompt(&#39;Please enter your name:&#39;,"")
 if (username!=null && username!="")
 {
 setCookie(&#39;username&#39;,username,365)
 }
 }
}
Copy after login

The above is the detailed content of Detailed explanation of how JavaScript determines whether to enable cookies and operate cookie codes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template