JavaScript How-to
Below is a classic example on how to use JavaScript to print text. I know this isn't very interesting, but it will be a good way to explain how JavaScript works and its basic syntax.
Example Code
The JavaScript below writes text JavaScript is Fun! to the page.
The Code
<script type="text/javascript">
<!--
document.write("JavaScript is Fun!");
//-->
</script>
The output is:
JavaScript is Fun!
Explanation of Code
- <script type="text/javascript"> and </script> tells where the JavaScript starts and ends.
- <!-- and //--> are regular HTML comment tags. These tags are optional, but recommended. They are used to "hide" the JavaScript, so browsers that don't support JavaScript (or with JavaScript disabled) will not display the code in plain text to the user. The two forward slashes at the end of comment line (//) is the Javascript comment symbol. This prevents JavaScript executing the //--> tag.
- document.write is a standard JavaScript command for writing output to a page.