JavaScript Basics

 

JavaScript Basics

This is part three of the JavaScript Tutorial. If you missed the previous lessons, you may want to review it prior to reviewing the JavaScript basics lesson.

So, let’s move forward and write our first JavaScript.

Writing Your First JavaScript

Open your NotePad or favorite text editor, and let’s get started. The first thing you are going to learn is how to make JavaScript write something to your HTML document. Within your text editor, write or copy and paste the following script:

<script type="text/javascript">
<!–
document.write("<i>HI Everybody!</i>");
//–>
</script>

This script will write the words, HI Everybody on your webpage. Now, this is a very simple script, and you can use HTML to write words on your page, but the basis of this script will be very important later on, so it is important to learn this one. Let’s look at each element of the script.

 
First, we are telling the web browser that it is about to see and interpret JavaScript with this tag:

<script type="text/javascript">

This is an important tag. The browser needs to know what it is about to see. The next part is really important as well. You see, not all browsers recognize JavaScript for what it is. They just see it as text that should be printed on a page, as it is. You have to prevent this from happening, by hiding the JavaScript from older browsers. This is done by using HTML comment tags, as seen below:

<!–Comment –>

Now, with the JavaScript hidden inside the comment tag, older browsers will ignore it, but newer browsers will see it for what it is, and if the browser is JavaScript enabled, it will run the script. If the browser is not JavaScript enabled, nothing will happen.

The next part of our script is:

document.write

The document is an object. We discussed this earlier, remember? Write is the method (or function) that we want to use on the document. It is presented with a period between the two words: document.write. The write method is actually presented as write() with what you want written inside the parentheses.

The text that we want written is called a string in JavaScript, and it is therefore enclosed in quotation marks. It is presented as this:

(<i>"HI Everybody!"</i>)

Note that we have also added a little HTML into the mix with the use of tags that will force the text to be printed in italics. We’ve written the object, the method, and the string so that now the script has everything it needs to run. We’ve also closed or ended the comment, and then the script with the following:

//–>
</script>

Congratulations! You’ve written your first JavaScript, and it is the basis for so many other wonderful things that can be done with JavaScript. Be sure that you paste this code into an HTML document, and load it up to your web server so you can see how it works.
 

Dealing With Non-JavaScript Enabled Browsers

As stated in the previous lesson, if a user’s browser is not JavaScript enabled, the script can’t run. Often, nothing will happen, but it is possible that the user will get error reports. This can be prevented by using the <noscript> JavaScript tag. It looks like this:

<noscript>
Your Browser Does Not Support JavaScript
</noscript>

So far, you have learned about objects, methods, and strings. You have learned how to write a very basic JavaScript, and you have learned how to hide JavaScript from old browsers, and how to handle browsers that do not have JavaScript enabled.

 
This concludes the JavaScript Basics lesson. In the next lesson, we will focus on JavaScript Events.