How to keep a cookie value when user visit other pages?

I want to maintain a cookie that is set to not to disappear when a user visit other pages. The situation in my code is that it gets a parameter of the page location and saves it into the / path.

Here’s my code:

window.onload = function() {
    try {
        var url_string = (window.location.href).toLowerCase();
        var url = new URL(url_string);
        // Get gclid,token or fbclid parameters
        var gclid = url.searchParams.get("gclid");
        var token = url.searchParams.get("token");
        var fbclid = url.searchParams.get("fbclid");

        // token expires in 6 hours
        document.cookie = `gclid=${gclid}; max-age=21600` + ";path=/";
        document.cookie = `token=${token}; max-age=21600` + ";path=/";
        document.cookie = `fbclid=${fbclid}; max-age=21600` + ";path=/";

    } catch (err) {
        console.log("Issues with Parsing URL Parameter's - " + err);
    }
}

Example:

www.test.com/contact?gclid=123   // saves the cookie value

When going to other pages www.test.com/about it also sets a value to null because the cookie is coming from the url parameters.

the gclid=123 is stored in a cookie in the path /. But when I go to other pages like wwww.test.com/about

the gclid cookie will be equal/set to null.

The situation here in my code is that everytime the user visits a pages the url.searchParams searches for the desired parameter and if it does found those parameter the cookie will be sit to null.

How do I keep the previous value of the cookie gclid if the new gclid contains a null value in my page? And updates the gclid if it does not contains a null value.

88 thoughts on “How to keep a cookie value when user visit other pages?”

  1. It is perfect time to make some plans for the future and it’s time to be happy.
    I have read this post and if I could I wish to suggest
    you some interesting things or advice. Maybe you could write next articles referring to this article.
    I wish to read even more things about it!

    Reply
  2. Hi would you mind sharing which blog platform you’re working with?
    I’m going to start my own blog soon but I’m having a tough time choosing between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique.
    P.S Apologies for getting off-topic but I had to ask!

    Reply
  3. I will immediately take hold of your rss feed as I
    can not find your email subscription link or newsletter service.
    Do you’ve any? Kindly allow me realize so that I may subscribe.
    Thanks.

    Reply
  4. Woah! I’m really loving the template/theme of this blog. It’s simple, yet effective.
    A lot of times it’s tough to get that “perfect balance”
    between user friendliness and visual appeal. I must say you have done a awesome job with this.

    Also, the blog loads very quick for me on Safari.
    Superb Blog!

    Reply
  5. Pingback: viagra generic
  6. Pingback: viagra online usa
  7. It’s perfect time to make some plans for the future and it is time to be happy.
    I have read this post and if I could I want
    to suggest you few interesting things or tips. Perhaps you could write next articles referring to this article.
    I wish to read even more things about it!

    Reply
  8. I’ve been browsing on-line greater than three hours as of late, but
    I by no means found any fascinating article like yours.
    It is beautiful worth enough for me. In my opinion, if all site owners and bloggers made just right content material as you
    did, the web shall be a lot more useful than ever before.

    Reply
  9. Hey! Someone in my Myspace group shared this website with us so I came to give it
    a look. I’m definitely loving the information. I’m book-marking and will be tweeting
    this to my followers! Exceptional blog and superb style and design.

    Reply
  10. Pingback: viagra online
  11. Pingback: cialis price
  12. Pingback: herbal viagra
  13. I am not sure where you are getting your information, but
    good topic. I needs to spend some time learning much more or understanding more.
    Thanks for great information I was looking for this information for my
    mission.

    Reply
  14. Pingback: cheap cialis pills
  15. 700632 961233Superb read, I just passed this onto a friend who was performing some research on that. And he really bought me lunch since I identified it for him smile So let me rephrase that: Thank you for lunch! 198513

    Reply
  16. Pingback: buy viagra dallas
  17. Pingback: viagra price 1mg
  18. Pingback: cialis peptide
  19. Pingback: sildenafil 20 mg
  20. Pingback: taking viagra
  21. Pingback: lady viagra doctor
  22. Pingback: cialis vs viagra
  23. Pingback: lady viagra
  24. Pingback: womens viagra
  25. Pingback: visual viagra
  26. Pingback: sildenafil citrate
  27. Pingback: viagra tablet
  28. Pingback: sildenafil pills
  29. Pingback: hims viagra
  30. Pingback: cialis viagra
  31. Pingback: viagra usa

Leave a Comment