title: Access a cookie that was set on a javascript file, but not on the HTML
body: <p>I have a script that needs some external information to work with. It fetches this using Ajax requests. So far so good.</p>
  
  <p>However, the script needs some of it's data right from the start. So I have been pondering a few options to supply it with that initial data at page load time:</p>
  
  <ol>
  <li>Simplest: Just have it perform an Ajax request for the data right away. Downside of this is extra latency and more requests than strictly needed.</li>
  <li>Ugly: Add a small script fragment at HTML render time that provides the initial data</li>
  <li>Bad caching properties: Create the whole JS file dynamically and add the data right then.</li>
  <li>Impossible: Something with headers... but unfortunately it seems we can't access them (see e.g. <a href="https://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript">this question</a>). Doing the extra Ajax request is not useful here as in that case we might just as well use option #1.</li>
  <li>Something with cookies...</li>
  <li>Not tried yet: Create a dynamic 'initial-data.js' script whose sole purpose it is to load the initial data. This would at least only send the data when needed, but it would require all users of my script to include 2 script files instead of one.... Also it will cause an extra request...</li>
  </ol>
  
  <p>I am trying out the 4th option of using cookies to transport the initial data but so far not having any success. What I am trying to do:</p>
  
  <ul>
  <li>When the browser requests the .js file, have the server add a Set-Cookie header with the initial data in it in the response.</li>
  <li>In the JS file, read out the cookie.</li>
  </ul>
  
  <p>It doesn't work. It seems I need to set the cookie on the response for the .html instead of the .js for the browser to make it available to the script... That's too bad as it would involve adding the Set-Cookie header to each page, even though it's only needed by that particular piece of JS. </p>
  
  <p>I was actually very happy with the solution I thought I found because it let me send the initial data along with the request for the script only to those pages that actually use the script... Too bad!</p>
  
  <p>Is there any way to do what I'm trying to do using cookies, headers or some similar mechanism?</p>
  
  <p>Do you guys have any tips for this situation?</p>
  
  <p>Background:
  I am trying to write a semi-offline application. Semi-offline in that it should continue to work (apart from some functions that just need connectivity) when offline, but is expected to have periods with connectivity regularly. So I'm using local storage and synching with the server when possible.</p>
  
  <p>To be able to have the client generate new items when offline, I am including an ID generator that gets handed out ID blocks by the server, consuming them as it generates ID's. The data I was trying to send to the script in a cookie is the initial list of ID blocks and some settings and looks like this:</p>
  
  <pre><code>/suid/suid.json:3:3:dxb,dyb,dzb
         ^        ^ ^      ^   
        url     min max   blocks
  </code></pre>
  
  <p>Where:</p>
  
  <pre><code>url = path to JSON for subsequent Ajax requests
  min = minimum amount of ID blocks to keep in local storage
  max = maximum amount of ID blocks to keep in local storage
  blocks = comma separated list of ID blocks
  </code></pre>
  
  <p>The ID blocks are encoded as sort-of Base32 strings. I'm using a custom formatting schema because I want 53-bit ID's to be as short as possible in text format while still being easily human readable and write-able and URL-safe.</p>