Post by RoGeR on Jul 22, 2008 3:46:21 GMT -5
Anyways, when learning to code, some good sources of information include many of the open source codes and tutorials located throughout all the different Proboards and the google search engine. You can very often Google a line, and usually you will recieve a tutorial or explanation on it.
But, moving right along. Well, basically, when coding with Javascript, you will have the opening tag:
<script type="text/javascript">
And the closing tag:
</script>
Very similar to HTML, so, when coding Javascript you will have something like this:
<script type="text/javascript">
</script>
Very difficult stuff, I know, hope I'm not going to fast?
Also, some people will also include a HTML comment tag at the beginning and end of their Javascript code (The look like <!-- and //-->). These are used to stop extremely old browsers, which don't recognize Javascript from actually displaying the content of the Javascript tags. You don't have to use them if you don't want to. Anyway, so now, we have something like this:
<script type="text/javascript">
<!--
//-->
</script>
We are also able to leave Javascript comment tags, which will not be processed by the code. These are usually used to include a copyright of some sort or to leave instructions on where to edit the code. There are two Javascript comment tags. One is used for single line comments and the other for multiple line comments.
Here is an example of a single line comment:
<script type="text/javascript">
<!--
//Anything in this line will not be processed by the code
//-->
</script>
Here we can see that the double slash '//' is used to start the single line comment. (There is no need to close or end the single line comment, you just need to start a new line)
However, if we wanted to have a multiple line comment, we would use:
<script type="text/javascript">
<!--
/*Anything within
these multiple lines
will not be processed*/
//-->
</script>
And as we can see here, the /* is used to signify the start of a multiple line comment, and the */ is used to signify the end.
Well, so far we have learnt how to make a Javascript code that does absolutely nothing! But don't get too excited yet.
Alrighty, so lets have our Javascript actually do something! We will start off with the document.write command. This will basically do as the name suggests and write something into your document. It will look something like this:
<script type="text/javascript">
<!--
//A code
document.write('What are you looking at?');
//-->
</script>
This nifty code will put the text What are you looking at? wherever you place the code. So basically whatever is put between the quotation marks will be displayed on the page. You are able to use the double quotation marks if you want " " it is just a matter of preference. Though, make sure whatever type of quotation you start with, you also end with. You are also able to include HTML within the quotation marks.
And now onto Variables! When using a variable, it will look something like this:
var VariableName="Content";
So, whenever we call upon the variable: VariableName, we will receive the text: Content.
So:
<script type="text/javascript">
<!--
//Variables!
var TheName="What are you looking at, again?";
document.write(TheName);
//-->
</script>
This code will write to our document, the Text: What are you looking at, again? The red text is the name of our variable. Notice, how when writing the variable to our document, we do not use the quotations.
You may have noticed, that after some lines in the code I have included a semicolon ;
var TheName="What are you looking at, again?";
document.write(TheName);
This is not a requirement and is absolutely optional. This is because in other programming languages ending a statement with a semicolon is required. As such, Javascript allows it to be used at the end of their statements. Though, by ending a javascript statement with a semicolon, one is able to change two lines of coding, into one, e.g. this:
var TheName="What are you looking at, again?";
document.write(TheName);
Could be turned into this:
var TheName="What are you looking at, again?";document.write(TheName);
Though, I guess I wouldn't recommend having two statements on the one line, it doesn't really achieve much, it just makes your code less comprehensible to others.
Anyways, so far we should have learnt the basics on:
<script type="text/javascript">
<!--
// Comment Tag
var TheName="Content of our variable";
document.write(TheName);
//-->
</script>
This powerful Javascript code should make your site more dynamic and marvelous than it's ever been!
Anyway, those are just the very basics. Hope you all enjoyed!
Credits to IAlex for this really good tutorial he wrote.
But, moving right along. Well, basically, when coding with Javascript, you will have the opening tag:
<script type="text/javascript">
And the closing tag:
</script>
Very similar to HTML, so, when coding Javascript you will have something like this:
<script type="text/javascript">
</script>
Very difficult stuff, I know, hope I'm not going to fast?
Also, some people will also include a HTML comment tag at the beginning and end of their Javascript code (The look like <!-- and //-->). These are used to stop extremely old browsers, which don't recognize Javascript from actually displaying the content of the Javascript tags. You don't have to use them if you don't want to. Anyway, so now, we have something like this:
<script type="text/javascript">
<!--
//-->
</script>
We are also able to leave Javascript comment tags, which will not be processed by the code. These are usually used to include a copyright of some sort or to leave instructions on where to edit the code. There are two Javascript comment tags. One is used for single line comments and the other for multiple line comments.
Here is an example of a single line comment:
<script type="text/javascript">
<!--
//Anything in this line will not be processed by the code
//-->
</script>
Here we can see that the double slash '//' is used to start the single line comment. (There is no need to close or end the single line comment, you just need to start a new line)
However, if we wanted to have a multiple line comment, we would use:
<script type="text/javascript">
<!--
/*Anything within
these multiple lines
will not be processed*/
//-->
</script>
And as we can see here, the /* is used to signify the start of a multiple line comment, and the */ is used to signify the end.
Well, so far we have learnt how to make a Javascript code that does absolutely nothing! But don't get too excited yet.
Alrighty, so lets have our Javascript actually do something! We will start off with the document.write command. This will basically do as the name suggests and write something into your document. It will look something like this:
<script type="text/javascript">
<!--
//A code
document.write('What are you looking at?');
//-->
</script>
This nifty code will put the text What are you looking at? wherever you place the code. So basically whatever is put between the quotation marks will be displayed on the page. You are able to use the double quotation marks if you want " " it is just a matter of preference. Though, make sure whatever type of quotation you start with, you also end with. You are also able to include HTML within the quotation marks.
And now onto Variables! When using a variable, it will look something like this:
var VariableName="Content";
So, whenever we call upon the variable: VariableName, we will receive the text: Content.
So:
<script type="text/javascript">
<!--
//Variables!
var TheName="What are you looking at, again?";
document.write(TheName);
//-->
</script>
This code will write to our document, the Text: What are you looking at, again? The red text is the name of our variable. Notice, how when writing the variable to our document, we do not use the quotations.
You may have noticed, that after some lines in the code I have included a semicolon ;
var TheName="What are you looking at, again?";
document.write(TheName);
This is not a requirement and is absolutely optional. This is because in other programming languages ending a statement with a semicolon is required. As such, Javascript allows it to be used at the end of their statements. Though, by ending a javascript statement with a semicolon, one is able to change two lines of coding, into one, e.g. this:
var TheName="What are you looking at, again?";
document.write(TheName);
Could be turned into this:
var TheName="What are you looking at, again?";document.write(TheName);
Though, I guess I wouldn't recommend having two statements on the one line, it doesn't really achieve much, it just makes your code less comprehensible to others.
Anyways, so far we should have learnt the basics on:
- How to start and end Javascript
- HTML Comment Tags
- Javascript comment tags
- Variables
- document.write
<script type="text/javascript">
<!--
// Comment Tag
var TheName="Content of our variable";
document.write(TheName);
//-->
</script>
This powerful Javascript code should make your site more dynamic and marvelous than it's ever been!
Anyway, those are just the very basics. Hope you all enjoyed!
Credits to IAlex for this really good tutorial he wrote.