Helping ordinary people create extraordinary websites!

JavaScript Getting Started

Letís write our first piece of JavaScript. The basic example of any language is "Hello World", so we'll start with displaying the text "Hello World" to the user. First, to do this, youíre going to need a text editor. Unlike word processing applications like Microsoft Word, a text editor helps you deal with raw code very effectively, and the best text editor is Notepad. Load up Notepad Ė itís probably in Start > Programs > Accessories. Type in the following:

<html>
<body>
<script>
document.write('Hello World!');
</script>
</body>
</html>

Save it as "javascript.html" and open it in your browser. You should see the following:

Hello World!

So, what exactly did this JavaScript do? Let's examine the code.

<html>
<body>
<script>

</script>
</body>
</html>

This part is really just the lead up to the JavaScript code. The <html> and <body> are common to any HTML web page. The <script> tells the web browser that we have written some JavaScript code and it needs to execute it.

<html>
<body>
<script>
document.write('Hello World!');
</script>
</body>
</html>

The document.write tells the web browser to write some text to the current document (or web page). Here we are calling the document.write function. There are many different functions available, and you can write your own too. A function can take some parameters that tell it what to do - in this case, we have one parameter, 'Hello World!'.

However, we can improve on this example just a little. JavaScript is not the only scripting language supported by the <script> tag, so we should tell the browser what language our script code is in, through the type attribute:

<html>
<body>
<script type="text/javascript">
document.write('Hello World!');
</script>
</body>
</html>

This simply tells the browser that the code will be in JavaScript. Whenever you want to try out JavaScript code,

Now let's take a look at JavaScript syntax.

« Introduction to JavaScript JavaScript Syntax »


More JavaScript Tutorials:
» Browser-Correct Download Instructions
» Looping through form elements using JavaScript
» Javascript Diff Algorithm
» Client Side Data Binding Using jQuery
» Restoring Form Field Values
» JavaScript - Formatted Date, Back, Forward Buttons
SPREAD THE WORD!
Tell a Friend

GET OUR NEWS

Developer Resources