Weeb tutorials feedback Powered by

Javascript – An easy introduction

Javascript is used to add interactivity and functionality to a site. It has many uses, from detecting the viewers web browser to validating form input.

It is compatible with all major web browsers, and is relatively easy to learn. This makes it one of(if not) the most popular scripting language on the internet.

In other words, if you are are web developer(or aim to be) it is in your best interest to start learning Javascript now!

Javascript is a client side scripting language, which means that the script is executed by the users web browser rather than the actual web server.

Just like a HTML document, when requested by the web browser the document is located and sent by the web server. It is then up to the web browser to execute the code

Javascript code is usually embedded within a HTML page, but can be written in an external file and referenced by the HTML document.

Some examples of Javascript usage:

  • Validating form data.
  • Confirmation or notification
  • Dynamically changing content
  • Adding tool-tips to your site
  • Interaction

Some simple Javascript Code

To insert a script into our HTML we use the opening <script> tag and the closing </script> tag.

Let's see a very simple script, the following will output “This is Javascript” to our web page.

<script type="text/javascript">
document.write("This is Javascript");
</script>

As you can see from the above code we need to declare a type when using the script tag. We do this by typing type="text/javscript". Just as we would if we were embedding CSS code into a html document.

You can have as many script tags in your HTML page as required. There is no limit to the amount of scripts you can have on one single page.

Javascript – Incompatible/Older browsers

Some older browsers may have problems reading or executing Javscript code. If a viewer is using one of these browsers the code will be displayed as content on your page, rather than being executed.

To get around this we use the HTML comment tag, this hides the script from the browser. Take a look below for an example of this:

<script type="text/javascript">
<!--
document.write("This is Javascript");
//-->
</script>

If you have used HTML comments before you will probably have noticed that our end tag //--> is slightly different from the normal comment closing tab -->.

The double forward slash is the Javascript comment tag. This prevents the Javascript from executing the code.

Things to remember

  1. Each piece of Javascript code, or each line of Javascript ends with a semicolon. You can see an example of this in our simple script.
  2. Javascript is case-sensative.Its probably a good idea to keep most of your code lower-case to avoid problems.
  3. Text should always be surrounded by double quotation marks.
This is the old version of our site. To visit the new version click the following link: New site