JS-Where to Place it

Since javascript isn't HTML, you will need to let the browser know in advance when you enter javascript to an HTML page. This is done using the <script> tag.

The browser will use the <script> type="text/javascript"> and </script> to tell where javascript starts and ends.

Consider this example:

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

<body>
<script type="text/javascript">
alert("Welcome to my world!!!");
</script>
</body>
</html>
 


The word alert is a standard javascript command that will cause an alert box to pop up on the screen. The visitor will need to click the "OK" button in the alert box to proceed. 





By entering the alert command between the 
<script type="text/javascript"> and </script> tags, the browser will recognize it as a javascript command.

If we had not entered the <script> tags, the browser would simply recognize it as pure text, and just write it on the screen.

You can enter javascript in both the <head> and <body> sections of the document.
In general however, it is advisable to keep as much as possible in the
 <head>section.

Post a Comment

Previous Post Next Post