Where to write Javascript?.

Writing javascript code.

  • As Javascript is a Client programming tool it must be included in a web-page.
  • The HTML offer an element, script, with the start tag <script> and the end tag </script> where you can include your Javascript code.
  • For the script element the HTML specification required that you must specify the "type" attribute. It could work without, which depend on the browser, but is is not recomended.
  • There is three ways to include your javascript coding:
    1. Directly inside the web-page

      Write your javascript directly inside the start tag <script> and the end tag </script>
      Example:
      <script type="text/javascript">
          document.write("I am learning Javascript!");
      </script>
    2. As an external resource

      Specifies a location with the "src" attribute where the browser will find an external file containing the javascripts.
      Example:
      <script type="text/javascript" src="Javascript/Example1.js">
      </script>
      In this case the external file including path specification, Javascript/Example1.js, contains:
      document.write("I am learning Javascript!");
    3. Javascript, HTML events

      You can write the script statements for all HTML events.
      Example:
        <body onclick="document.write('Testing onclick event')" >
         click on me!
        </body>
  • If it is not given any special requirements, it is normal to add script element with JavaScript code within the HTML head element.
  • You can specify as many script elements on the same web-page as you want, but remember they will be executed in the order they are included.

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.