How to add JavaScript into HTML page

Just open a HTML file with any html editor software or notepad. You can use Notepad ++ for batter result. JavaScripts in HTML must be inserted between <script> and </script> tags.

JavaScripts can be put in the <body> and in the <head> section of an HTML page. To insert a JavaScript into an HTML page, use the <script> tag.
The <script> and </script> tells where the JavaScript starts and ends.
The lines between the <script> and </script> contain the JavaScript from the below  box:




<html><head><title>My Page</title>
<script language="javascript" type="text/javascript">
function myFunction() {
          alert('Hello world');
}
</script>
</head>
<body>
<a href="javascript:myFunction();">Click here</a>
</body>
</html>


Since the head loads before the body, placing code in the head ensures that it is available when needed. For example, the following code will work once the page has completely loaded, but if a user manages to click the link before the function has loaded they will get an error. This can easily become an issue if the page is large or slow to load.

<html><head><title>My Page</title>

</head>
<body>
<a href="javascript:myFunction();">Click here</a>
<script language="javascript" type="text/javascript">
function myFunction() {
                alert('Hello world');
}
</script>
</body>
</html>

Note: JavaScript can cause problems when used in tables. It may help to use JavaScript to create the entire table, or at least whole rows.