Including JavaScript Code


<script type="text/javascript">   window.alert("Welcome to JavaScript!"); </script> 

JavaScript code can come in two ways: either embedded into an HTML page, or in an external file. The most common way to include JavaScript code is to use the <script> element. You can place this element anywhere, and the code is then executed after this part of the HTML page has been loaded and parsed. The preceding code (file script.html in the download archive) opens a modal window and prints some rather simple text.

The type attribute provides the MIME type for JavaScript. Previously, language="JavaScript" was used; however, since that was not standardized, it is considered best practice to use type and the MIME type instead. In this book, we follow the approach many websites are using nowadays: Both type and language are used.

Also, back in the old days, it was possible to target a script to a specific version number of JavaScript, like this:

<script language="JavaScript1.6">   window.alert("Only with JavaScript 1.6!"); </script> 


However, this is almost never used now. First of all, implementation of this feature has been quite buggy in browsers, and there are better ways of testing a browser's JavaScript capabilities.

Note

In some old tutorials you will find the advice to use HTML comments in the following fashion:


<script language="JavaScript"><!--   // ... //--></script> 


Note

This was previously used to cope with browsers that did not know anything of JavaScript. However, even browsers that do not support JavaScript know about the <script> element and know to ignore it (including its contents). Therefore, these HTML comments are not necessary any longer.





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net