Re: linking JavaScript
by Andrew McFarland <aamcf(at)aamcf.co.uk>
|
Date: |
Wed, 24 Jul 2002 18:05:43 +0100 |
To: |
<hwg-techniques(at)mail.hwg.org> |
References: |
|
|
todo: View
Thread,
Original
|
|
At 13:48 23/07/02 -0700, Roger Harness wrote:
>Are there any cons to using linked JavaScript files, instead of embedded
>ones?
I have only heard of one, and I've never experienced it. Some browsers
incorrectly fire the onload event handler before the external files are
loaded, which can cause problems if the onload calls a function in a linked
file. However, I've never experienced this.
There are several advantages to putting JavaScript in external files, such
as cachability and maintainability, that others have mentioned. One
additional advantage that it makes it easier to write valid XHTML.
Traditionally embedded JavaScript has been hidden from non-JavaScript
parsers using comments:
<.script type="text/javascript">
<!--
alert('!');
// -->
<./script>
Unfortunately, XHTML requires content like this to be `hidden' another way:
<.script type="text/javascript">
<![CDATA[
alert('!');
]]>
<./script>
These two techniques are incompatible. The first will make the script
available to JavaScript aware HTML browsers but unavailable to JavaScript
aware XML browsers. The second will have the opposite effect.[1]
When you link to the JavaScript you don't have this problem - no funny
characters in the XHTML, no worries.
It also makes more sense to link to files rather than embedding. After all,
you don't embed GIF data in the HTML, why should you embed JavaScript?
Andrew
--
http://aamcf.co.uk/
[1] Combining them has the effect of preventing both types of browser for
getting to the JavaScript.
HWG hwg-techniques mailing list archives,
maintained by Webmasters @ IWA