Lesson 3: JavaScript Tags



If you are familiar with HTML then you should be well acquainted with the concept of tags. In JavaScript, there are some important tags you will need in order to be able to use your code within an HTML document. First of all there is the JavaScript opening and closing tags that will let the browser know that it has encountered a script. Here is an example all nicely wrapped up in an HTML script, also note the two different types of comment tags used:




    • <html>

    • <head>

    • <title>JavaScript-HTML Example</title>

    • <script type="text/JavaScript">

    • <!--






    • //Notice the forward slashes for 1

    • //line quotes within the JavaScript.






    • /* here is another way to mark a comment that

    • we use if the comment is more than one line

    • long(begin with"/* end with "*/")

    • */

    • //


    -->

    • </script>

    • </head>

    • <body>

    • HTML goes here

    • </body>

    • </html>



Okay, you should know what lines 1-3 are so lets skip to line 4. Line 4 is the opening script tag: <script type="text/JavaScript"> which is closed in line 15: </script>. This lets the browser know that the script that it is processing is JavaScript. Next, in line 5, we have our tag used to make the code invisible to older browsers (<!--) which is closed in line 14 (//-->). For those who may not remember from studying HTML, those are HTML comment tags with 2 forward slashes added before the HTML closing comment tag. By putting the entire JavaScript snippet inside of HTML comment tags, we have effectively made the code invisible to even older browsers without JavaScript. Then of course lines 16 - 20 complete our HTML document.


Now let's make ourselves a template so we do not have to type in all of the tags every time we want to test a block of code. You can write your own using what you have learned so far or copy and paste the following code to your text editor:


<html>

<head>

<title>Code Template</title>

<script type="text/JavaScript">

<!--

 

//-->

</script>

</head>

<body>

<script type="text/JavaScript">

<!--

 

//-->

</script>

</body>

</html>


*Notice that there are two places where you could insert your JavaScript code in the above template.* Now save this as your template. I save mine as JStemplate.txt.

                    

Copyright © 2012 VisualBuilder. All rights reserved