Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8051389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:17:37+00:00 2026-06-05T07:17:37+00:00

I am seeing lots and lots of functions for reading cookies in via java

  • 0

I am seeing lots and lots of functions for reading cookies in via java script, but I only want to use it once and inside a variable, I am new to JS.

Here is my code

var TheNumber = (Math.random() + '') * 1000000000000000000;
document.cookie= "rand=" + TheNumber.toString() + ";path=/";
var AdServer = {
    tile: 1,
    mock: false,
    ord: (Math.random() + "") * 1000000000000000000 + '?',

I want to replace the ord part with the value from the rand cookie.

Could you guide me on the following:
Do I need a function?
If so where do I put it?
How would I call it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:17:38+00:00Added an answer on June 5, 2026 at 7:17 am

    I find the easiest (and most flexible) way to write/read from cookies with JavaScript is with global object with getter/setter methods.

    The Mozilla dev docs page on document.cookie has a well documented example: https://developer.mozilla.org/en/DOM/document.cookie

    How/where you might instantiate and then reference that object depends on the rest of your program, but assuming for simplicity we’re just in the global namespace and aren’t worried about variable collision etc:

    var docCookies = {
        getItem: function (sKey) {  
          if (!sKey || !this.hasItem(sKey)) { return null; }  
          return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1")); 
        },
        setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {  
          if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/.test(sKey)) { return; }  
          var sExpires = "";  
          if (vEnd) {  
            switch (typeof vEnd) {  
              case "number": sExpires = "; max-age=" + vEnd; break;  
              case "string": sExpires = "; expires=" + vEnd; break;  
              case "object": if (vEnd.hasOwnProperty("toGMTString")) { sExpires = "; expires=" + vEnd.toGMTString();  } break;  
            }  
          }  
          document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");  
        },
      hasItem: function (sKey) { return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); }  
    }
    

    Then set your cookie with:

    docCookies.setItem('rand', (Math.random()* 1000000000000000000).toString());
    

    And get it with:

    docCookies.getItem('rand');
    

    So to put it all together:

    var docCookies = {
        getItem: function (sKey) {  
          if (!sKey || !this.hasItem(sKey)) { return null; }  
          return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1")); 
        },
        setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {  
          if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/.test(sKey)) { return; }  
          var sExpires = "";  
          if (vEnd) {  
            switch (typeof vEnd) {  
              case "number": sExpires = "; max-age=" + vEnd; break;  
              case "string": sExpires = "; expires=" + vEnd; break;  
              case "object": if (vEnd.hasOwnProperty("toGMTString")) { sExpires = "; expires=" + vEnd.toGMTString();  } break;  
            }  
          }  
          document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");  
        },
      hasItem: function (sKey) { return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); }  
    }
    //set our cookie
    docCookies.setItem('rand', (Math.random()* 1000000000000000000).toString());
    

    then later/elsewhere in your code when you want to retrieve the cookie value:

    var AdServer = {
        tile: 1,
        mock: false,
        ord: docCookies.getItem('rand')
    };
    

    Now if you inspect AdSever.ord it will equal the random number from your rand cookie that you set earlier.

    console.log(AdServer.ord);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seeing lots of SOAP/RDS stuff, but just want to dump some MySQL data and/or
i need to use a charting library and after seeing lots of charting API
For some reason we seem to be seeing lots of 404 errors with all
I have read lots these past few weeks on IE6, seeing if it was
I've lots of varying definitions for the following terms, but I'm not clear as
Hey guys I have done lots of work with ASIHTTPRequest so far and use
Lots available on this subject but I havn't found anything that answers my question...
I'm seeing lots of applications using hashes as surrogate keys instead of plain integers.
So I'm seeing a lot of C# software engineer job ads asking for lots
Django is a great framework, but after seeing a couple of learning videos I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.