Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Scorpian

Scorpian Avatar

******
[ Bracket Admin ]

2,457


April 2006
Are you a noob? Do you have no clue what JavaScript does? Do you want to learn? Fear not, for I am here to help! I'll guide you through all of the basics of JavaScript in these few, simple tutorials. :)

First off, it's imperitive that you never get Java & JavaScript confused. Yes, they have similar names. However, that's the only similarity. They are two completely different languages that are meant to do completely different things. When NetScape made JavaScript, they decided to name it that since Java was already popular. It helped JavaScript grow pretty well. Now, just about every browser on the planet supports it.

First off, let's learn how to set-up a code tag. You can do this by two ways.
1.) You can embed your code with script tags.
2.) You can remotely host it.

It doesn't matter which way you use, as you'll need to use script tags for both. So, let's learn how to set them up. A script tag is just an HTML element that let's the browser know that it needs to read everything in it as another type of script. It's mainly used for JavaScript. The layout is pretty simple.

<script type='text/javascript' src='myscript.js'></script>

I have color-coded this for you. The red parts are the script tags themselves. This is what tells the browser to stop reading HTML, and read a type of script. The blue part tells the browser that the type of script it's going to be reading is JavaScript in a text format. Now, the green part is optional. If you remotely host your code, this is what tells the browser where it can find the script. It's used just like when you link to a stylesheet or image. Leave it out if you're not using a seperate script.

Now, let's learn the basics. Here's a code with some pretty simple elements of JavaScript that we'll learn:

<script type='text/javascript'>
// This line is a comment

/* This is all
a comment */


var varName = "Variable Value";
</script>


Alright, the maroon parts are the opening & closing tags of our JavaScript segment. Everything you want to write in JavaScript goes between them. The gray parts are comments. If you want to write something that the browser shouldn't pay attention to, you can comment it out, just like you comment in HTML. For a one-line comment, use //. For a multiple-line comment, start it with /*, and end it with */.

The bold navy part is what we use to define a varaible. Simply start a line with "var", followed by a space, and put the variable name, shown in black. Next, we'll add an equal sign, followed by the value of the variable. This is the part in blue, surrounded by quotes. Some things to note about variables is that you can combine different things into a variable using a + sign. For example:

var myName = "Scorpian";
var welcomeMessage = "Welcome, " + myName + ", to our site!";


This defines "Scorpian" as the value of myName. It then defines welcomeMessage as "Welcome, ", then whatever the value of myName is, and ", to our site!". This is very useful in simplifying scripts.

You can also define multiple variables in one line if they all have the same value. For example:

var myMoney = myFunds = "3";

That shows that the variable myMoney will equal whatever the value of myFunds is, which we define as three. You may feel free to define as may variables as you want this way. Just seperate them with equal signs.

Well, that sums up the most basic of the basics for this tutorial. Next tutorial, we'll learn how to use these variables. We'll also learn the infamous alert box, and how to use it. Stay tuned, folks. ;)
wat

newBookmarkLockedFalling