|
||||||||
|
|||||||
|
|
Last updated: 06 Oct 2008
Placing javascripts inline (together with page markup) or in separate files
There are two distinct ways to add javascripts to your page. You can
put javascripts inside your HTML markup (inline) or into separate files on
your web server.
The inline method doesn't require the browser to make a separate request for the javascript source body, allowing it to execute immediately. To place inline javascript into page add the following to your HTML: <script type="text/javascript"><!-- ...your javascript code here... //--></script>The HTML comment tags <!-- and //--> are to prevent browsers that don't understand scripts from displaying the script code. The normal behaviour of the browsers that don't understand a tag is to render its contents as if the tag wasn't there. All modern browsers running on computers will understand this tag, however some obscure handheld devices or phones might not. Putting javascript sources into separate files requires the browsers to make separate requests to web server to get the scripts. Therefore this method might increase the page load time unless the script code is already in the cache. Modern browsers and techniques of HTTP/1.1 (such as Keepalive and HTTP pipelining) have increased the performance of putting scripts into separate files. To reference javascript file in page add the following to your HTML: <script type="text/javascript" src="specify script file URL here"> </script>Normally you want to place your script in separate file if at least some of your users will visit more than one page with the same script. The javascript samples on this site are shown using the inline method. However, they can be converted to separate files in the following way:
<script type="text/javascript"><!-- //--></script>
<script type="text/javascript" src="specify script file URL here"> </script> | ||||||||||||||
| |||||||