1.

Why to avoid global variables in JavaScript

Answer»

Place the JavaScript anywhere WITHIN your web page, but it is recommended to include it within the <head> or <body> tag.

The following are the different ways in which we can include JavaScript code in an HTML

In head section

If you WANT the SCRIPT to run on some event, then add the JavaScript code in the head section of the HTML file.

Let us see an example:

<html>    <head>       <script>          <!--             function show() {                alert("Demo!")             }          //-->       </script>    </head>    <body>       <input TYPE="button" onclick="show()" value="Click" />    </body> </html>

In body section

If you want the script to run on page load, then add the JavaScript code in the body section of the HTML file. Let us see an example:

<html>    <head>    </head>    <body>       <script>          <!--             document.write("Demo")          //-->       </script>       <p></p>    </body> </html>


Discussion

No Comment Found