JS Cookie Use method detailed

Source: Internet
Author: User
Tags set cookie setcookie net domain

The code is as follows

<script>
function GetCookie (c_name) {
if (document.cookie.length>0) {///First query whether the cookie is empty, return ""
C_start=document.cookie.indexof (c_name + "=")//through the String object's IndexOf ()

Check to see if this cookie exists, it does not exist-1
if (c_start!=-1) {
C_start=c_start + c_name.length+1//finally this +1 actually means "=" number, so

Gets the starting position of the cookie value
C_end=document.cookie.indexof (";", C_start)//Actually I just saw IndexOf () the second

parameter is suddenly a bit dizzy, then remembered to indicate the location of the specified start index ... This sentence is to get the end position of the value. Because

Need to consider whether it is the last item, so pass ";" Whether the number exists to judge
if (c_end==-1) c_end=document.cookie.length
Return unescape (document.cookie.substring (c_start,c_end))/Through

SUBSTRING () Gets the value. To understand Unescape () is to know what escape () is to do, is a very important basis, want to understand

Can be searched below, at the end of the article will also be explained cookie coding details
}
}
Return ""
}


function Setcookie (c_name, value, Expiredays) {
var exdate=new Date ();
Exdate.setdate (exdate.getdate () + expiredays);
Document.cookie=c_name+ "=" + Escape (value) + "path=/;d omain=45it.net" +

((expiredays==null)? "": "; expires=" +exdate.togmtstring ());
}


function SC (days)
{
Setcookie (' ashow ', ' a ', days);
document.getElementById (' ad '). style.display= ' None ';
}

Function GC ()
{
if (GetCookie (' ashow ') ==1)
{
Alert (GetCookie (' ashow '));
document.getElementById (' ad '). style.display= ' None ';
}
}


</script>

<div id= "AD" >fdsafdsafdsafsda</div>
<script>
GC ();
</script>
<a href= "#" onclick= "JAVASCRIPT:SC (1);" > Set Cookie Value </a>
<a href= "#" onclick= "JAVASCRIPT:GC ();" > Get Cookie value </a>
<a href= "#" onclick= "JAVASCRIPT:SC (-1);" > Clear Cookie Value </a>

Summary of calling Cookies yourself

Path= this if you don't have the settings then your cookie can only be applied to the current directory, such as

http://www.3lian.net/php/phper.html if we get it on other pages like http://www.3lian.net/sj/

Not to this directory cookie value

Set method

path=/can be.

Domain= this is to set the domain name, such as we set the www.3lian.net domain name, not set we can only in this domain name, then

The level two domain name is unable to read the cookie value.

Set up

Domain=3lian.net

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.