Slide 11.b: JavaScript (cont.)
Slide 11.d: JavaScript where to …
Home

JavaScript How to …


To insert a JavaScript into an HTML page, we use the <script> tag (also use the type attribute to define the scripting language).

So, the <script type="text/javascript"> and </script> tells where the JavaScript starts and ends.
 <html>
  <body>
   <script type="text/javascript">
    ...
   </script>
  </body>
 </html>

The client-side object document contains information about the current document, and provides methods for displaying HTML output to the user. The method document.write is for writing output to a page.

By entering the document.write command between the <script> and </script> tags, the browser will recognize it as a JavaScript command and execute the code line.
 <html><body>
  <script type="text/javascript">
   document.write( document.referrer )
  </script>
 </body></html>

In this case the browser will write the URL of the calling document to the page by using the property document.referrer. Note that if we had not entered the <script> tag, the browser would have treated the document.write( document.referrer ) command as pure text, and just write the entire line on the page.

Ending Statements with a Semicolon?
With traditional programming languages, like C++ and Java, each code statement has to end with a semicolon. Many programmers continue this habit when writing JavaScript, but in general, semicolons are optional! However, semicolons are required if you want to put more than one statement on a single line.

Demonstration