|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
There Is No Magic: JavaScript Tutorial
Finally I've found the time to do something I planned to do
for long long time: writing tutorial for JavaScript, based on my personal experience with JavaScript. Keep in mind that my tutorial is not official and there might be better ways to do some of the things. If you happen to notice such thing then please say something so that I can both learn and improve this tutorial. There will be several parts, I will post each part as reply in this thread and keep updated index here in the first post. Here we go then! ![]() Introduction to JavaScript What? Why? How? What is it? JavaScript is client side scripting language. This one sentence contains three phrases that need further explanation:
What is the meaning of the language name? It consist of two words:
Why JavaScript? For various reasons:
How do I start? Under Windows OS click Start --> Run --> type "notepad" in the textbox and click OK. For non Windows OS just open your favorite text editor. Copy and paste the following code into the text editor: Code:
<html>
<head>
<title>The Magic Of JavaScript</title>
<script type="text/javascript">
alert("hello world");
</script>
</head>
<body>
This page will eventually contain some nice things that can be done with JavaScript.
</body>
</html>
Double click the file to launch your default browser and execute it. If you have IE7 you'll have to click the yellow bar that appear on top of the contents and choose "Allow Blocked Content". You should see JavaScript alert box with the message "hello world". Congratulations! You've just wrote custom JavaScript code that is fully under your control. Keep tuned to this article to learn the real stuff. ![]() Coming next: Going Technical, Difference between JavaScript and VBScript and answer to questions. If you have any comments or questions, don't be shy! Post comment and say it. Tutorial Index Introduction to JavaScript Getting Technical With JavaScript Happy Programming! Last edited by Shadow Wizard : December 2nd, 2008 at 06:30 AM. Reason: added index |
|
#2
|
||||
|
||||
|
JavaScript Tutorial part II
Getting Technical With JavaScript
In this part we'll discuss ALF: Arrays, Loops and Functions, elements that are the heart of any programming language. I wish to thank Don for making this tutorial use the best language possible - I could not achieve this without you, Don! ![]() Arrays First, a quick reminder for those who forgot:
The thing that distinguishes between those two types is the index. The common array has an integer index, meaning you reference an item using a numeric index and the second type of array can have anything as an index, including complex objects and elements. More on this below.
Loops Quick reminder:
Thousand words will make it crystal clear: (hopefully) Code:
<script type="text/javascript">
var myString = "";
for (var i = 1; i <= 9; i++)
{
myString += i;
}
alert(myString);
</script>
What happened here? We iterated over the numbers 1 to 9, and in each iteration added the current number to string variable. The code inside the loop (that adds value to the string) was executed 9 times, because the initial value was 1 and the stop condition was when the loop iterator is bigger than 9. (More accurately, the loop was running as long as the loop iterator was equal or smaller than 9) As you see, a loop does not have to start from 0. It can start from anything you like (not ever a number!) and can end with any condition you like. Consider this code: Code:
<script type="text/javascript">
var myString = "";
var dayMS = (1000 * 60 * 60 * 24);
var dtIterator = new Date();
var curMonth = dtIterator.getMonth();
while (dtIterator.getMonth() == curMonth)
{
myString += dtIterator + "\n";
dtIterator = new Date(dtIterator.getTime() + dayMS);
}
alert(myString);
</script>
This is another kind of loop, with different syntax but same logic. The loop iterator here is a variable with the name dtIterator. The initial value is the current date, and in every loop iteration we're adding one day to the loop iterator. When we reach the next month, we stop. Finally, we just show the dates from the current date to the last day of the month. The above just demonstrates that loop can have many forms, and the important part is that loop will always have section of code repeating itself and loop iterator that decides when the loop ends. That's it for now about loops: if you have any question or something you wish to know, just ask. Functions Function is block of code that can be called and executed from anywhere in the code, including from other functions. The block of code is wrapped inside the function, and you give name to that function, then in order to execute the wrapped code just use the name of the function. A function can get as many arguments as we like, and return one single return value, of any type. (Number, String, Date or even complex type) In JavaScript, function also act as class: it can wrap its own properties and methods. This is not commonly known though. Classic example is function that is getting two numbers as input (arguments) and returns the sum of those two numbers: Code:
<script type="text/javascript">
function MyFirstFunction(num1, num2)
{
return num1 + num2;
}
alert(MyFirstFunction(10, 20));
</script>
As expected, you'll see dialog box with the number 30: our first function has two arguments: num1 and num2, and is returning the sum of those arguments. Now I'm going to introduce the other side: using functions as classes in JavaScript. Here you go: Code:
<script type="text/javascript">
function Car(strModel, strColor, nInitialMileage)
{
this.model = strModel;
this.color = strColor;
this.mileage = nInitialMileage;
this.Drive = function CarDrive(nMilesCount)
{
this.mileage += nMilesCount;
}
this.GetModel = function CarGetModel()
{
return this.model;
}
this.GetColor = function CarGetColor()
{
return this.color;
}
this.GetCurrentMileage = function CarGetMileage()
{
return this.mileage;
}
}
var myFirstCar = new Car("Volvo", "black", 1500);
var mySecondCar = new Car("Jaguar", "metallic blue", 12500);
myFirstCar.Drive(500);
alert("my first car is " + myFirstCar.GetModel() + " and it drove total of " + myFirstCar.GetCurrentMileage() + " miles and my second car color is " + mySecondCar.GetColor() + " and it's mileage is " + mySecondCar.GetCurrentMileage());
</script>
What happened here? Don't worry, it's no magic so I'll explain step by step.
![]() As a bonus for those reading so far down the page, here is code that combines all the three parts of ALF to do something cool. Just copy into NoMagic.html and execute! Code:
<script type="text/javascript">
function MyFirstCoolFunction(someString)
{
var arrFuncReturnValue = new Array();
for (var i = 0; i < someString.length; i++)
{
var curChar = someString.substr(i, 1);
if (typeof arrFuncReturnValue[curChar] == "undefined")
{
arrFuncReturnValue[curChar] = 0;
}
arrFuncReturnValue[curChar]++;
}
return arrFuncReturnValue;
}
var myArray = MyFirstCoolFunction("hello world, how are you today? Wonder what is going to happen...");
var sMessage = "";
for (var character in myArray)
{
sMessage += "The character " + character + " appeared in the string " + myArray[character] + " times.\n";
}
alert(sMessage);
</script>
We combined arrays, loops and functions to create something that might have actual use one day: counting how many times each character appears in a given string. Coming next: Interacting with the user, if..then tutorial and maybe some more details about stuff we've already learned. If you have any comments or questions, don't be shy! Post comment and say it. Tutorial Index Introduction to JavaScript Getting Technical With JavaScript Happy Programming! Last edited by Shadow Wizard : December 2nd, 2008 at 06:31 AM. Reason: added tutorial index |
![]() |
| Viewing: ASP Free Forums > Programming > HTML, JavaScript And CSS Help > There Is No Magic: JavaScript Tutorial |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|