3

I was searching for an answer here and found that it an be accomplished through webstorage and cookies. However, I also came across this interesting post:

Do browsers parse javascript on every page load?

It says: "Chrome 42 introduces an advanced technique of storing a local copy of the compiled code, so that when the user returns to the page the downloading, parsing, and compiling steps can all be skipped. Across all page loads, this allows Chrome to avoid about 40% of compile time and saves precious battery on mobile devices."

Does it mean that values of variables will be persistent across page reloads? For eg. if I am storing the color selected by the user in a variable, then will that value be available in that variable after another page (on same domain) is loaded?

P.S: I could not comment there as I am a newbie and don't have enough reputation required for commenting on answers.

Community
  • 1
  • 1
darKnight
  • 3,344
  • 8
  • 30
  • 59

1 Answers1

2

No, it does not mean variables are stored, or that HTML suddenly has state, it only means Chrome stores the parsed scripts internally so they don't have to be downloaded and parsed again.

This is just an internal optimization to make Chrome faster, not something you have acccess to, you'll still need persistent storage, like cookies, local storage or the server.

adeneo
  • 293,187
  • 26
  • 361
  • 361
  • Thanks for your answer. Few more questions: Is parsing same as rendering/manipulating the DOM tree by JS? If yes, then how can the browser use the same pre-parsed JS for different pages? (as DOM gets reconstructed on each page load) If not,then kindly explain the diff b/w parsing and rendering Also, where are values of variables stored in the browser? Is it attached to the DOM tree or is there a separate memory area reserved by browser for JS? (Here I am not referring to storing properties of HTML elements like color which might get changed by JS and which are part of the DOM tree) – darKnight Apr 06 '15 at 11:36
  • That's very technical, but parsing the script isn't the same as rendering, the browser will usually have a javascript engine and a rendering engine that works together. How exactly Chrome does these things internally is to complicated for me to understand, but it stores the scripts and probably some data to know what the result of the parsing was, so it doesn't have to do it again. On first pageload the script parsing will generally always be the same, the DOM is the same etc. Variables are stored in memory, and as HTML is stateless that memory is released on a new pageload. – adeneo Apr 06 '15 at 11:46
  • So everything that is done with javascript is always lost when reloading a page or redirecting to another page, there is no state. Cookies, local storage etc. is the only way to keep data across page loads. – adeneo Apr 06 '15 at 11:48