Exforsys

H I D E

Home arrow Technical Training arrow JavaScript Tutorial

How to use JavaScript in HTML page

Author : Exforsys Inc.     Published on: 20th Apr 2007    |   Last Updated on: 24th Dec 2010

How to use JavaScript in HTML page

In this JavaScript tutorial you will learn how to use JavaScript in HTML page, how to write output to a page, different places where JavaScript can be paced in HTML, JavaScript in external file and how to place and execute JavaScript in external file.

How to Insert JavaScript into a HTML page:

You can insert JavaScript into an HTML page by using <script> tag.

JavaScript is placed between tags starting with <script type = text/javascript> and ending with </script>.

General syntax of JavaScript is as follows:


<
html>
   <body>
    
<script type="text/javascript">
        ...
//JavaScript

     </script>
   </body>
</html>

How to write output to a page:

The JavaScript command used for writing output to a page is document.write.  The document.write command takes the argument as the text required for producing output. The example below shows how to place JavaScript command inside an HTML page :

Example:


<
html>
   <body>
    
<script type="text/javascript">
       
document.write("EXFORSYS Training")
     </script>
   </body>
</html>

Output of the above script as produced in a HTML page is shown below:

Observe that semicolons have not been placed to mark the end of statement, which differs from code writing style in programming languages C and C++. In JavaScript, the placement of a semicolon is optional. If a programmer wants to place more than one statement on a single line then the programmer can make use of the semicolon.

Different places where JavaScript can be paced in HTML:

JavaScript can be placed in various locations in HTML such as

  • JavaScript in HEAD section
  • JavaScript in BODY section
  • JavaScript in both HEAD and BODY section
  • JavaScript in External File

The placing of JavaScript in the above location differs in the timing of their execution. JavaScript placed in the HEAD section of HTML will be executed when called whereas, JavaScript placed in the BODY section of HTML will be executed only when the page is loaded.

The general structure for placing a JavaScript in the HEAD section is:


<
html>
   <head>
    
<script type="text/javascript">
        .....
        .....
//JavaScript written in HEAD Section

     </script>
   </head>

   <body>
   </
body>
</html>

The general structure for placing a JavaScript in the BODY section is


<
html>
   <head>
   </head>

   <body>
     <script type="text/javascript">
        .....
        .....
//JavaScript written in BODY Section

     </script>
   </body>
</html>

If a programmer wants to execute JavaScript when called, or when an event is triggered, then JavaScript is placed in the HEAD section.  JavaScript is placed in the HEAD Section because scripts gets loaded first. When a programmer wants to execute JavaScript when the page loads then JavaScript should be placed in the BODY section.  In addition to these, you can also place JavaScripts in both HEAD and BODY sections of an HTML page.  Moreover, you can place any number of scripts in an HTML page.

The general structure of JavaScript for placement in both head and body sections is as follows:


<
html>
   <head>
    
<script type="text/javascript">
       
//JavaScript written in HEAD Section
       
document.write("JavaScript placed in HEAD Section")
     </script>  

   </head>

   <body>
     <script type="text/javascript">
       
//JavaScript written in BODY Section
       
document.write("JavaScript placed in BODY Section")
     </script>
   </body>
</html>

Output of the above script as produced in a HTML page is shown below:

JavaScript in External File:

There may be scenarios in which the same functionality of script needs to be executed in several places in program. For handling this scenario instead of writing the same JavaScript in several places which would cause poor optimization of code one can place the JavaScript in external file. 

How to place the JavaScript in External file:

This process is very simple. The code or the JavaScript which needs to be executed in several places in the program is written separately in a file and is saved with extension as .js for the file.

How to execute this JavaScript placed in External File:

This is done by using the attribute named as src for the <script> tag.

Te general syntax of this is as follows:


<
html>
   <head>
    
<script src="filename">
     </script>  

   </head>

   <body>
   </
body>
</html>

Ads

The script tag is placed as needed in either HEAD section or BODY section as seen in earlier chapter but with the src attribute as follows:

It is important to note that external JavaScript file cannot contain HTML tags.

Suppose if the JavaScript placed in external file is named as exforsys.js then it is executed by placing it as


<
html>
   <head>
    
<script src="exforsys.js">
     </script>  

   </head>

   <body>
   </
body>
</html>

Read Next: JavaScript Conditional Statements Part 1


 
This tutorial is part of a JavaScript Tutorial tutorial series. Read it from the beginning and learn yourself.

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction
 

Comments