1

Will doing a full page postback clear javascript variables etc from memory?

Joe
  • 41,484
  • 20
  • 104
  • 125
JoeS
  • 1,405
  • 17
  • 30
  • 2
    Yes it will, any page reload is a fresh start in regards to javascript. – adeneo Nov 26 '13 at 15:50
  • What about garbage collection especially with latest js technologies like webgl ? http://stackoverflow.com/questions/11286451/browser-refresh-does-not-do-garbage-collection Just stackoverflow by all means..:) – Roy M J Nov 26 '13 at 15:53
  • Is "full page postback" a commonly used phrase? Is a postback just an HTTP request to the server? What's the difference between a full and a partial postback? – Felix Kling Nov 26 '13 at 15:53
  • 1
    As far as I know: a full post back actually POSTs the form back to the server. A partial postback uses javascript to fire and AJAX request to get the data back to the server. – JoeS Nov 26 '13 at 15:58

2 Answers2

2

In computing, a stateless protocol is a communications protocol that treats each request as an independent transaction that is unrelated to any previous request so that the communication consists of independent pairs of requests and responses.

A stateless protocol does not require the server to retain session information or status about each communications partner for the duration of multiple requests. In contrast, a protocol which requires keeping of the internal state on the server is known as a stateful protocol.

Examples of stateless protocols include the Internet Protocol (IP) which is the foundation for the Internet, and the Hypertext Transfer Protocol (HTTP) which is the foundation of data communication for the World Wide Web.

As HTTP is stateless, so is javascript when used in a browser, with some exceptions, such as Ajax calls. No variables are kept in memory between page reloads, unless you use some sort of persistent storage, such as cookies, localStorage, sessionStorage, indexedDB etc. or use the server to store data about the current session or something similar.

In other words, each time a page is loaded, the styles and scripts are downloaded from the server or gotten from the cache if they are cached, and executed again, for every single pageload.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Though you can make a HTTP request without loosing JavaScript state: with Ajax. I'm just mentioning this because your answer sounds like that JavaScript being stateless is a consequence of HTTP being stateless, which I'd argue it is not. – Felix Kling Nov 26 '13 at 16:05
  • @FelixKling - That is true, but then you're not reloading the current page, you're making a XMLHttpRequest to another page without the current page reloading, and that's why the state is kept ? – adeneo Nov 26 '13 at 16:10
  • Exactly. I mean, it makes that in the browser context, JavaScript state is tied to how HTTP works, but technically it doesn't have to be. And XMLHTTPRequests are one those the exceptions. – Felix Kling Nov 26 '13 at 16:13
  • 1
    That's also very true, javascript is used many places outside a browser, and some of those places the concept of state doesn't even apply, and as noted, ajax is an exception, and then there's things like sockets and workers, that aren't really HTTP requests, but they still let you execute external code etc. without reloads. Added something to the answer to make that at least somewhat clearer. – adeneo Nov 26 '13 at 16:19
1

Yes, it will since when we doing a full page postback, we send the page back to the server and we wait to respond, sending us the page again. Then each script on the page or each referred script, will load from the start. So all the javascript variables would be reinitialized.

Christos
  • 53,228
  • 8
  • 76
  • 108