Book Image

Object-Oriented JavaScript

Book Image

Object-Oriented JavaScript

Overview of this book

Table of Contents (18 chapters)
Object-Oriented JavaScript
Credits
About the Author
About the Reviewers
Preface
Built-in Functions
Regular Expressions
Index

Including JavaScript in an HTML Page


In order to include JavaScript in an HTML page, you need to use the <script> tag:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head> 
    <title>JS test</title>
    <script type="text/javascript" src="somefile.js"></script>
  </head> 
  <body> 
    <script type="text/javascript"> 
       var a = 1;
       a++;
    </script>
  </body> 
</html> 

In this example, the first <script> tag includes an external file, somefile.js, which contains JavaScript code. The second <script> tag includes the JavaScript code directly in the HTML code of the page. In both cases, the <script> tag takes a type attribute, which is required in XHTML 1.0, although the code will work even without it. The browser executes the JavaScript in the sequence it finds it in the page. This means that if you define...