Ways of Adding JavaScript code

Following are the ways to add JavaScript in html page.

  1. Inline : JavaScript code that we writes inside html tags are called inline JavaScript, useful to write short code in JavaScript.
    Example:

    <html>
    <body>
    <input type=”button” onclick=”alert(‘hello’)” value=”Hello”/>
    <input type=”button” value=”Delete” onclick=”return confirm(‘Sure To Delete’)”/>
    </body>
    </html>
  2. Internal : JavaScript written in same html page inside script tags are called internal JavaScript.
    Example:

    <html>
    <script>
    function hello()
    {
    alert(“hello internal”);
    }
    </script>
    <body>
    <input type=”button” onclick=”hello()” value=”Hello”/>
    </body>
    </html>
  3. External : JavaScript written in some other ‘.js’ file called external JavaScript. External JavaScript file can be use in more than one file html file.
    Example:
    Code in ‘myJava.js’ file

    function hello()
    {
    alert(“hello external”);
    }

    Code in ‘myHtml.html’ file

    <html>
    <script src=”myJava.js” type=”text/javascript”> </script>
    <body>
    <input type=”button” onclick=”hello()” value=”Hello”/>
    </body>
    </html>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.