Java Reference
In-Depth Information
Figure 1-2
Where Do My Scripts Go?
Enough talk about the subject of JavaScript; it's time to look at how to put it into your web page. In this
section, you'll fi nd out how you add JavaScript code to your web page.
Including the type attribute is good practice, but within a web page it can be left off. Browsers such
as IE and Firefox use JavaScript as their default script language. This means that if the browser
encounters a <script> tag with no type attribute set, it assumes that the script block is written in
JavaScript. However, use of the type attribute is specifi ed as mandatory by W3C (the World Wide Web
Consortium), which sets the standards for HTML.
Linking to an External JavaScript File
The <script> tag has another arrow in its quiver: the ability to specify that the JavaScript code is not
inside the web page but inside a separate fi le. Any external fi les should be given the fi le extension .js.
Though it's not compulsory, it does make it easier for you to work out what is contained in each of your
fi les.
To link to an external JavaScript fi le, you need to create a <script> tag as described earlier and use its
src attribute to specify the location of the external fi le. For example, imagine you've created a fi le called
MyCommonFunctions.js that you wish to link to, and the fi le is in the same directory as your web page.
The <script> tag would look like this:
<script type=”text/javascript” src=”MyCommonFunctions.js”></script>
The web browser will read this code and include the fi le contents as part of your web page. When link-
ing to external fi les, you must not put any code within the <script> tags; for example, the following
would be invalid:
<script type=”text/javascript” src=”MyCommonFunctions.js”>
var myVariable;
Search WWH ::




Custom Search