Simple Machine Codes
March 28, 2024, 06:14:59 pm
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Welcome to SMC, the best place for your SMF For Free Codes
 
  Home Help Search Arcade Gallery Affiliates Code Index Staff List Calendar Members Login Register  

Tutorial #2: Variables

Pages: [1]   Go Down
  Print  
Author Topic: Tutorial #2: Variables  (Read 661 times)
0 Members and 1 Guest are viewing this topic.
Agent Moose
Administrator

Offline Offline

Gender: Male
Posts: 2,672



View Profile WWW
« on: January 01, 2008, 12:18:49 am »

What is a Variable?
A variable is a "container" for information you want to store. A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value.

Ok, How do I use them?
There are two ways you can start out a variable.  You can use this;
Code:
<script>
var name = value
</script>

Or, you can use a shorter way:
Code:
<script>
name = value
</script>

It doesn't matter which one you use.

A very inportant thing, you may have a variable named whatever you want, as long as there are no spaces.  So, you can use this:
Code:
<script>
var myVariable = "Hello There"
</script>
or this:
Code:
<script>
var my_variable = "Hello There"
</script>
But you can't use this:
Code:
<script>
var my ariable = "Hello There"
</script>

What exactly would I use them for?
As you read above, variables are "containers".  They can hold HTML, or text, or even other scripts just as Jquery and Javascript codes.

Here is an example of useing a variable for text...
Code:
<script>
name = "Text is being Used";
</script>

Just a little note, the ";", it is good to have that at the end of everyline when ever you are doing javascript, but you don't have to.  It just says that that line is done.

Now, that will not show anywhere because it doesn't have anything to make it show.  To make it you would use this...
Code:
<script>
name = "Text is being Used";
document.write(name);
</script>
As you can see, the document.write() is holding the variable name, "name".  So, the text that would show would be "Text is being Used" since that is the text in the variable.

You may use HTML in the variables.  Just one thing you need to remember, you can't use double quotes, you must use Single Quotes.

Now for somthing that is a bit harder.
Code:
<script>
var my_spans = document.getElementsByTagName("span");
document.write(my_spans.length);
</script>
The variable "my_spans" is getting all the tags with SPAN in them, and then writing how many there are on that page.  If you change span to table, tr, td, a, or any other tag, it would do the same exact thing, but the number may be diffrent.

Now useing the variables in an if...else statement.
Code:
<script>
var x = "2";

if(x == "2"){
  alert("That is the right number.");
}else{
  alert("Not the right number.");
};
</script>
In the above code, the variable is x, and it has the value of 1.  The if statement is reading the variable, and seeing if is it equal 2(The == means "is equal to").  Since x is equal to 2, then you will get an alert saying "That is the right number.".  If the variable was equal to something else, then you will get the alert "Not the right number.".
« Last Edit: February 04, 2008, 04:26:02 am by Omikron 9861 » Report Spam   Logged

Share on Facebook Share on Twitter

Myrtle
The Girl With The Forum.
100 Club Member

Offline Offline

Gender: Female
Posts: 480


Death Note(Credits to Kai): Myrtle_Gail, Myr-chan


View Profile WWW
« Reply #1 on: February 25, 2008, 02:17:53 am »

Thanks for the tut Moose. I got most of it except that I was lost at the end/hard part. But at least I'm starting well.
Report Spam   Logged

Blahs
100 Club Member

Offline Offline

Posts: 184


Rockstar!


View Profile WWW
« Reply #2 on: March 18, 2008, 02:28:11 pm »

Great guide I knew this but if i didn't i could learn from it...
Report Spam   Logged


Join my forums!!!!
Url: http://silabforums.smfforfree4.com/index.php
robo123
Starter

Offline Offline

Posts: 24


View Profile
« Reply #3 on: May 07, 2008, 11:18:30 am »

Thanks that helped me loads. nice tut. Cool
Report Spam   Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum

Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy