JavaScript - Placement in HTML file


JavaScript can be written by two method

  1. External javascript
  2. Internal javascript

 

1) External javascript

In the external javascript, a javascript file is created externally and save this file with the extension of  '.js '.

After creating a javascript file we need to add a javascript path to HTML file using  <script src="write full file path address.js") </script>  inside  the head tag.

 

For example

<!DOCTYPE html>

<html>

  <head>

    <title>Teknowize</title>

    <script src="E:/javafile.js"> </script>

  </head>

<body>

  

</body>

</html>

 

2) Internal javascript

In internal javascript, the javascript code may be written inside the head tag or we can also write it inside the body tag.

 

 For example

<!DOCTYPE html>

<html>

  <head>

    <title>Teknowize</title>

<!-- script written inside head tag -->

    <script type = "text/javascript">

        <!--

           function sayHello() {

              alert("Hello World")

           }

        //-->

     </script>

  </head>

<body>

<!-- script written inside body tag -->

    <script type = "text/javascript">

        <!--

           document.write("Hello World")

        //-->

     </script>

       <input type = "button" onclick = "sayHello()" value = "Say Hello" />


 </body>

</html>