Search Helium

Home > Computers & Technology > Internet > Web Design

Introduction to Javascript

by Wendy Connick

Created on: April 01, 2009

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 your 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 should 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.

Functions are the building blocks of Javascript programs. A function is a section of code that will run when called, either if a predetermined event takes place (say, a visitor types something into a text box) or if another function calls it. A complicated Javascript program might have

Helium Debate

Cast your vote!

Should social networking sites be regulated?

Click for your side.

242491

Featured Partner

Teachers Without Borders (TWB)

Teachers Without Borders (TWB) has partnered with Helium, giving you the chance to write for a cause. Browse TWB's featured titles, pick an issue and write! You can also donate your article earnings. Share what you know, l...more


CONNECT WITH US

Read
our blog
Helum for writers

Write and get published
Share with other writers
Polish your freelancing skills

Join our active writing community
Helium Content Source for Publishers

Quality articles from proven freelancers
Exclusive rights, fast turnaround
Brand engagement, business blogging -- our writers do it all

Get custom content today!

INFORMATION


Helium, Inc.
200 Brickstone Square Andover, MA 01810 USA
#