<er************ @gmail.comwrote :
I have a couple of questions regarding the loading of .js files
for the browser and would like anyone to point me wher to find
the answer (or if you know the answer and tell me will do just
fine ;) ) - If I have several pages all using 'somejs.js' file
this file is shared on disk
There is not much sense in "shared on disk" here. There is no necessary
relationship between a web server's responses to requests and the file
structure on the machine that is running it.
and is downloaded only once...
Presumably that it once per browser connection? However, each page that
references an external JS resource may request that resource anew form
the server. If they don't do so then the implication is that the JS is
cached on the client (which is mostly what would be expected) and
requests are being served from the browser's disc cache
but how about in memory,
is it also shared and just loaded once?
Javascript files are source code, which is used to define an executable
structure. A browser receiving a listing of javascript source code from a
server may store that in its disk cache for use with later requests
(possibly in later sessions), it may also elect to keep the source code
in memory for the current session in anticipation of further requests for
the same source.
For any given HTML page loaded into a browser a SCRIPT element's
reference to an external JS resource will necessitate the fetching of the
source code so that it can be processed into an executable for that
particular page. That source code may be fetched from the browser's
memory, the browser's disk cache, or the network (where it may be found
on an intermediate HTTP cache, or retrieved from the original server).
The executable structures created from the source code belong to the
individual page's representation in the browser. They cannot be shared
between pages displayed in the same browser (frameset frames or different
tabs) because they have a particular scope structure that relates to the
individual page. However, some browsers cache the page's representation,
so if you leave a page and then use the 'back' button to return to it
that representation can be re-used instead of needing to be re-built from
the original (HTML/CSS/JS) source code.
-Also, when are the .js files unloaded from memory?
The unloading of the javascript source files from memory (if they were
stored there more than fleetingly in the first place) is something that
can be left to the browser. The unloading of the executable structures
created from javascript source should follow the destruction of the
browser's representation of the page, which may follow the user's leaving
the page, or may be delayed until the browser is closed down.
-Can I unload at any given time a .js file from memory?
That is not under your control. You may influence the browser's (and the
network's) caching behaviour with appropriate HTTP headers. For example,
if an HTTP header states that a resource will expire in 10 minutes a
client may see no need to keep it in its cache (memory or disk) beyond
that point, as it should not re-use an expired resource.
The executable structures in association with a particular page's
reprehension are subject to javascript's normal garbage collection. If
you remove all references to the referenceable aspects of those
structures from the reprehension then the structures become available for
garbage collection and should be removed from memory at some subsequent
point.
Richard.