title: best way to send JSON to the page at creation time
body: <p>I need to send some data to a web page, ideally in json format and I wonder what method is considered best, and why. Overall what good or bad experiences and surprises you had with them.</p>
  
  <ol>
  <li><p><code>&lt;script&gt;var myJson = &lt;? echo json_encode($myVar);
  ?&gt;;&lt;/script&gt;</code></p>
  
  <p>advantage: the json is directly in javascript, were it will be used.<br>
  inconvenient: <code>&lt;script&gt;</code> in the middle of html/dom is bad (js belong
  to .js files).<br></p></li>
  <li><p><code>&lt;div data-myJson='&lt;? echo json_encode($myVar); ?&gt;'&gt;</code></p>
  
  <p>advantage: html5 data thing is easy to work with.<br> inconvenient:
  bunch of data in the dom, it doesn't look elegant note: in <em>my</em>
  case, I can afford to ignore "old" browsers.</p></li>
  <li><p>ajax everything.</p>
  
  <p>advantage: the json doesn't even need to be sent in this case, as it
  will be already available (no page change).<br>  inconvenient: not
  really an option as I would need to rewrite the full website.</p></li>
  <li><p>instead of sending the full json, store it in the session and send a
      key. </p>
  
  <p>advantage: less data moving around<br> inconvenient: the
      data/session couple needs to be kept track of, and I like my session to be kept clean and tidy. (even if user just close the page before the flow is
      finished) (which won't close the session).</p></li>
  <li><p>Cookies. </p>
  
  <p>advantage: herr.. is reverse evil a good thing?  <br>inconvenient: like session variables, but out of the cage.</p></li>
  <li><p>Store the json in the session, and ajax it when the page is loaded.</p>
  
  <p>advantage: somewhat elegant conceptually. <br>inconvenient: heavy, as the ajax instruction has
  to be added to a js file, and the session has to be managed. (and
  cleansed. if the page load doesn't finish, the json will stay until
  I cleanse it or the session finishes). Plus the html header means more bandwidth, and the we have to wait for the success to use the object.</p></li>
  <li>other?</li>
  </ol>
  
  <p>edit: as there seems to be a bit of confusion, with option 3 "ajax everything" I was meaning <em>one</em> page load, and <em>all</em> content loaded by ajax, even if you go through menus, links to other pages, forms submit, and such. I consider a more traditional navigation, (pages sent by the server as new a pages), with a page doing an ajax request to retrieve some value (here, my json object) on the server, as point 4 "session", as the main data has to remain on the server after the page has been sent to be later fetched by the ajax request. I did add option 6 for this.</p>