1. WELCOME
2. BASICS
4. OPERATORS
3. ADVANCED TOPICS
2. INHERITANCE
3. CLOSURES
1. MODULES
2. NAMESPACES
4. OPTIMIZATION
6. BOOKMARKLETS
4. BROWSER BUGS
6. ADVANCED WEB CLIENT PROGRAMMING
4. HANDLING XML
5. DESIGN MODE
7. JAVASCRIPT OUTSIDE HTML
9. APPENDICES
1. INDEX
2. LINKS
5. CONTRIBUTORS
LINKS
TO GO THROUGH IN THE NEXT POST in part 2
Linking to external scripts
JavaScript is commonly stored in a file so that it may
be used by many web pages on your site. This makes it much easier for updates
to occur and saves space on your server. This method is recommended for separating behavior (JavaScript) from content ((X)HTML) and it prevents the issue of
incompatibility with inline comments in XHTML and HTML.
Add src="script.js" to the opening script tag. Replace script.js
with the path to the .js file containing the JavaScript.
Because the server provides the content type when the
file is requested, specifying the type is optional when linking to external
scripts. It's still advised to specify the type as text/javascript, in case the
server isn't set up correctly, and to prevent HTML validation complaints.
<script
type="text/javascript" src="script.js"> </script>
Location of script elements
The script element may appear almost anywhere within the HTML file.
A standard location is within the head element. Placement within
the body however is allowed.
<!DOCTYPE
html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Web page title</title>
<script type="text/javascript"
src="script.js"></script>
</head>
<body>
<!-- HTML
code here -->
</body>
</html>
There are however some best
practices for speeding up your web site [4] from the Yahoo! Developer Network that specify a
different placement for scripts, to put scripts at the bottom, just before the </body> tag. This speeds up downloading, and also allows for
direct manipulation of the DOM while the page is loading.
<!DOCTYPE
html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Web page title</title>
</head>
<body>
<!-- HTML
code here -->
<script
type="text/javascript" src="script.js"></script>
</body>
</html>
No comments:
Post a Comment