About cookies need to set path and cookie domain

Source: Internet
Author: User
Tags set cookie setcookie split

Today encountered a small problem, in the invocation of the cookie, Document.cookie = ", Baidu found that because I set the cookie is not set the path of the cookie, so only in the current directory page access to cookies, I want the cookie to be accessible at the parent or even all pages, I must set the path of the cookie, PATH =/. New Encapsulated Cookie:

Set Cookie,name is the name to be saved, value is the corresponding value, Iday is the expiration
function Setcookie (name,value,iday,path) {
    //Use Date Object
    var Odate = new Date ();
    Set Expiration Time
    odate.setdate (odate.getdate () + iday);
    Set the cookie
    document.  = name + ' = ' + value + '; expires= ' + Odate + '; Path= ' + path;
}
Invoke the cookie for subsequent operation
function GetCookie (name) {
    //split cookie becomes array
    document.  Cookies. Split ('; ');
    for (var i = 0; i < arr.length; i++) {
        //continue to split Name=value
        var arr2 = arr[i].  Split(' = ');
        if (arr2[0] = = name) {
            return arr2[1]
        }
    }
    return '
}
//delete cookie
function Removecookie (name) {
    //Set cookie expiry time to-1
    setcookie (name, 1,-1)
}
This makes it possible to access cookies on all pages.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.