This is a general question about web browser behaviour.
Will a web browser cache document level Javascript written in the head? Or is it only if it’s in an include? Like a <script src="something" />
in the head?
This is a general question about web browser behaviour.
Will a web browser cache document level Javascript written in the head? Or is it only if it’s in an include? Like a <script src="something" />
in the head?
That would be a yes on both counts.
The html file itself will be cached, which contains script blocks and references to external scripts - and those external scripts can be cached too.
More details than you may want can be obtained from the masters of web optimization - Google.
Of course you really ought to have the JavaScript at the bottom of the body - unless it is one of the few one line scripts that need to run before the page loads.
Why is that?
From his website - http://javascriptexample.net/basics28.php
To add to the answers above, just be aware that JS posted directly in the head (or at the end of the body) of a document, between <script>
tags, won’t be of use on other pages unless it’s included on them in the same way (which means re-downloading it every time). So if it’s needed on multiple pages, link to it externally via a script
element, as then the code won’t have to be downloaded each time as it will be properly cached.
Good call. PHP is writing out the JS using a header_draw function but it might as well just spit out an external include for the above reasons. Thanks!
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.