Javascript Caching

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.

1 Like

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.

1 Like

Why is that?

From his website -

1 Like

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.

1 Like

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!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.