Slavo Smutny wrote on 21 nov 2005 in comp.lang.javascript
:
Hi,
which is better approach to store my JavaScript code, to store it in
separate .js file
or to embed the code within HTML attributes (e.g. <p
onclick="javascript:submit();">click this</p>)?
onclick="javascript:submit();"
is always wrong because superfluous, use:
onclick='submit();'
You cannot put the above in a .js file however.
perhaps you mean: <script ..></script> ?
I am interested in performance, i.e. how quick is page loaded in each
case?
Did you test it?
I think the better one is to store the code in separate .js file so
that the user browser can cache the file
(source: http://www.quirksmode.org/js/placejs.html#link1) but I am not
entirely sure.
The catching is true, but "better" depends on the file-size, the importance
of loading time, and the effect on server use amount.
In general the first reason to have a .js file is using that same file for
multiple pages, IMHO, with many advantages, like the ones above, but also
disadvantages, like extensive testing when you change the file.
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)