Javascript Basics
Javascript is a great way to add some interactivity to your website. With HTML alone your pages will be completely static, but if you throw in a few Javascript snippets you can have a site that will respond to your visitors' actions and a whole lot more.
One thing that tends to confuse web developers is that Javascript and Java sound a lot alike. Actually, they have nothing in common! Java is a programming language, while Javascript is strictly a scripting tool – it's not really complex enough to be considered its own language.
When you're ready to add some Javascript to your website, you have several options. First, you can write the code in its own page, and then call it in the webpage's header – this works much like adding a CSS page, except that you must also add a command somewhere within the webpage's body to activate the script. Second, you can write the script in the webpage's own header; again, you'll need to add some code to the page body to run the script. And third, you can write the code directly into the page body. This option is feasible for very short scripts only – otherwise you'll be cluttering up your HTML pretty badly!
Wherever you add your Javascript code, you'll need to tell the browser that what you are writing is to be interpreted as Javascript. You do this by placing the code between the following tags:
<script></script>
For full HTML compliance, it's best to add an attribute to the script tag so that your visitor's browsers know for certain it's Javascript and not some other script:
<script type="text/javascript"></script>
Finally, to be completely on the safe side it's wise to comment out everything between the script tags. That way if you have a visitor who happens to be running a prehistoric browser, they won't end up with a screen full of garbage. So your web page's Javascript will look something like this:
<script type="text/javascript">
<!--
Code goes here…
//-->
</script>
And speaking of comments, you can add comments to Javascript much as you can with HTML and CSS. In Javascript, comments are marked with // at the beginning of the line. That's why you'd include the two slashes before the end comment in the above example.
One thing that tends to confuse web developers is that Javascript and Java sound a lot alike. Actually, they have nothing in common! Java is a programming language, while Javascript is strictly a scripting tool – it's not really complex enough to be considered its own language.
When you're ready to add some Javascript to your website, you have several options. First, you can write the code in its own page, and then call it in the webpage's header – this works much like adding a CSS page, except that you must also add a command somewhere within the webpage's body to activate the script. Second, you can write the script in the webpage's own header; again, you'll need to add some code to the page body to run the script. And third, you can write the code directly into the page body. This option is feasible for very short scripts only – otherwise you'll be cluttering up your HTML pretty badly!
Wherever you add your Javascript code, you'll need to tell the browser that what you are writing is to be interpreted as Javascript. You do this by placing the code between the following tags:
<script></script>
For full HTML compliance, it's best to add an attribute to the script tag so that your visitor's browsers know for certain it's Javascript and not some other script:
<script type="text/javascript"></script>
Finally, to be completely on the safe side it's wise to comment out everything between the script tags. That way if you have a visitor who happens to be running a prehistoric browser, they won't end up with a screen full of garbage. So your web page's Javascript will look something like this:
<script type="text/javascript">
<!--
Code goes here…
//-->
</script>
And speaking of comments, you can add comments to Javascript much as you can with HTML and CSS. In Javascript, comments are marked with // at the beginning of the line. That's why you'd include the two slashes before the end comment in the above example.
This site needs an editor - click to learn more!
Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map
Content copyright © 2023 by Elizabeth Connick. All rights reserved.
This content was written by Elizabeth Connick. If you wish to use this content in any manner, you need written permission. Contact
BellaOnline Administration
for details.