Ralpharama Archive. The archive is almost 20 years old, this website is no longer maintained.

Home | New Writing | Old Writing | Cooking | Technical | Wine Making | Cocktails
   
 
Article

Javascript 101

Hits 2653 | Created 2007-06-13 | Modified 2007-06-13

This was going to be a long tutorial, but I only made it to Hello World...



Section 01 Article 01

Hello World
The first thing to do with a new language is to display Hello World however you can. It fills you with joy that you can actually do something so quickly.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Javascript 1.1</title>
</head>

<body>

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

</body>
</html>

Most of the script is HTML of course, so you should create a text file called 01-01.html with this in it, then view it in a web browser.

You should see:

Hello World!

Oh joy. I'm not going into the HTML here, as that's a different matter. However, later we'll be manipulating HTML elements with Javascript to do interesting things.

Run-down
<script type="text/javascript">

This declares a section of javascript. Until the browser reaches the close script tags, it will treat everything after this as code.

<!--

This is an HTML comment. It stops old browsers that don't understand Javascript from simply displaying the entire code as HTML. It also needs to be closed at the end of the script.

	document.write("Hello World!"); 

This is actual javascript. 'document' refers to this page. When we use a full stop after an entity, we mean that the next entity is a property of the first. The next entity can be a command, like 'write'. So then, we say 'this document, write this xxxx' The text passed to the command is in parenthesis. The string itself is in quotes, to identify it as a string.

We could also say:

	var myString = "Hello World!";
	document.write(myString);

And get the same result. 'var' is short for variable, and is basically our own little entity that we assign a string of text to. We can then pass our entity name to the 'write' command, and it will write the value of out entity for us.

Note
Semi-colons. Some say they are optional, and they are, but, if I were you, I'd terminate each line/command with one. If you continue in programming further, you'll understand why.

-->
</script> 

Close the HTML comment line, and the script tag. After this the browser will process everything as HTML as usual.

Note
You can have as many snippets of Javascript as you like, anywhere you like. Every time you want to insert code, just do so, like above.





Make a Comment

Name
Comment


Antispam Code (always a number)

Enter Code Above
Submit

Note: All HTML will be removed except for bold and italics. Links will not display. Line breaks are added automatically. Use <pre>...</pre> for code.

Tags

This item has the following tags:
tech javascript

Useful? Donate for hosting costs

All content copyright (c) Ralpharama