Post by Triad on Nov 16, 2008 15:23:04 GMT -5
This tutorial very briefly covers the basics of JavaScript by using basic vocabulary and showing you how to get started. I'll show you how to actually make something happen in the following tutorials.
Every script that is placed directly into the page source must contain the following tags around it.
<script type="text/javascript">
<!--
//-->
</script>
The red part is optional but recommended as it acts as an HTML comment which comments out all the code in browsers that are incapable of running JavaScript. There will always be that one person who is using an ancient browser, and that's what this is for.
Variables: This is where your high school algebra comes into play. Every programming and scripting language uses them because they are absolutely necessary. A variable simply holds a value that can be accessed or set when needed.
Use the following syntax to declare a variable.
var variable_name;
All this does is create the variable by telling the browser to create a place for it in your memory. To initialize, or give it a value, just use the = sign.
variable_name = "This is a string";
This initializes the variable called "variable_name" with a string of characters. In this case, the string of characters make up a sentence, but you can have HTML or CSS etc. as well.
Here's how you would put it all together.
<script type="text/javascript">
<!--
/*
This is a multi-line comment
that can go as long as you want.
*/
// Declare and initialize the variable
var text = "This is a string";
// Write the text to the document.
document.write(text);
//-->
</script>
The red lines are multi-line and one line comments which are used for notes when scripting. Whenever you start writing codes that are lengthy, you will always want to use them so you can remember what you were doing when you come back to it.
Also, always remember that each statement or line of code ends with a semi-colon. This is where most errors appear when people just begin coding because they forget or misuse them. You don't place them after loops or if statements that open and close with {}. Those are blocks of code that I'll discuss in the next tutorial.
The last thing to show you is concatenation, or the linking of items together.
In Javascript this is very simple as you just use the + sign.
<script type="text/javascript">
<!--
// Declare and initialize our text variable.
var text = "This is a string";
var begin = "<b>";
var end = "</b>";
// Outputs the sentence in bold
document.write(begin + text + end);
// You don't have to use variables.
document.write("<b>" + text + "</b>");
// You don't have to use variables.
document.write("<b>This is a string</b>");
//-->
</script>
Everyone of those statements will output the exact same thing.
This was very short, but it's just to get the brain thinking about how it works by having some basic knowledge.
Every script that is placed directly into the page source must contain the following tags around it.
<script type="text/javascript">
<!--
//-->
</script>
The red part is optional but recommended as it acts as an HTML comment which comments out all the code in browsers that are incapable of running JavaScript. There will always be that one person who is using an ancient browser, and that's what this is for.
Variables: This is where your high school algebra comes into play. Every programming and scripting language uses them because they are absolutely necessary. A variable simply holds a value that can be accessed or set when needed.
Use the following syntax to declare a variable.
var variable_name;
All this does is create the variable by telling the browser to create a place for it in your memory. To initialize, or give it a value, just use the = sign.
variable_name = "This is a string";
This initializes the variable called "variable_name" with a string of characters. In this case, the string of characters make up a sentence, but you can have HTML or CSS etc. as well.
Here's how you would put it all together.
<script type="text/javascript">
<!--
/*
This is a multi-line comment
that can go as long as you want.
*/
// Declare and initialize the variable
var text = "This is a string";
// Write the text to the document.
document.write(text);
//-->
</script>
The red lines are multi-line and one line comments which are used for notes when scripting. Whenever you start writing codes that are lengthy, you will always want to use them so you can remember what you were doing when you come back to it.
Also, always remember that each statement or line of code ends with a semi-colon. This is where most errors appear when people just begin coding because they forget or misuse them. You don't place them after loops or if statements that open and close with {}. Those are blocks of code that I'll discuss in the next tutorial.
The last thing to show you is concatenation, or the linking of items together.
In Javascript this is very simple as you just use the + sign.
<script type="text/javascript">
<!--
// Declare and initialize our text variable.
var text = "This is a string";
var begin = "<b>";
var end = "</b>";
// Outputs the sentence in bold
document.write(begin + text + end);
// You don't have to use variables.
document.write("<b>" + text + "</b>");
// You don't have to use variables.
document.write("<b>This is a string</b>");
//-->
</script>
Everyone of those statements will output the exact same thing.
This was very short, but it's just to get the brain thinking about how it works by having some basic knowledge.