I have visited many publications on
the Internet and have used some of them to choose and prepare the lessons
for my students. Thank you to all authors, who has published free their
work. My work is FREE for all on the net, too. I hope this Tutorial will
help to everyone who wants to make attractive web pages! Enjoy! The
main lessons are:
What
is Java Script?
It is a scripting language developed by Netscape to add
interactivity and power to web documents. Examples
of Java Script include live clocks, rollover effects, scrollers,
and so on. JavaScript differs from most other programmning languages
in that it is relatively easy to master,
even for people who have absolutely no programming
experiences whatsoever.
Why learn JavaScript?
With html, you are restricted to creatingstatic, non
interactive webpages. This, in today's internet standards, is unacceptable.
With JavaScript, you can change that.
Getting Started: Setting Up your
code.
Where do your JavaScript codes go? ..anywhere inside
the <html> tags of your page. The beginning of your code begins
with <script> and ends with </script>
<html>
<head>
<title>example</title>
</head>
<body>
<script language="JavaScript">
<!--
document.write("This text is written using JavaScript.")
//-->
</script>
</body>
</html>
The tags <!-- and //--> are comment tags
and should always be included to help hide your code against older browsers.
The only "functional part" of this script is the document.write("....")part.
OBJECTS
The script document.write("I am learning JavaScript")
write a line of text.
"document" is the object
in the above example.
"write" is the method of
this object.
JavaScript is a language of objects and almost all of
them have both methods and properties.
Document Object:
Properties
bgColor (changes background color)
lastModified (gives date of document's last change)
document.write (gives date of document's last change)
referrer (gives URL of the page that linked here)
fgColor (changes foreground color)
|
Methods
write (writes something)
writeln (writes in new line) |
FUNCTIONS
They achieve more complex task and are not executed
until you call upon them.
The basic syntax of a function is:
function name
{
function's code
}
Example:
function test()
{
document.write("Hello,
World!")
}
It will be not enough to put this in the <script></<script>
tags so to see the text on the screen. The function must be implement by
test()
This function doesn't use parameters.
In the next example they are used:
<html>
<head>
<title>Example</title>
</head>
<body>
<script>
var x=prompt("Please
enter your age")
function days(age)
{
var time=age*365
alert("You
have lived"+time+"days")
}
days(x)
</script>
</body>
</html>
The above function doesn't return a value. It only
take a value and use it to
show the result on the screen.
There are many functions which can return a value for
future using.
Lets see the example:
<html>
<head>
<title>function</title>
</head>
<body>
<script>
var a=prompt("Please
enter the length of the rectangle: ")
var b=prompt("Please
enter the width of the rectangle: ")
function area(a,b)
{
var
S=a*b
return
S
}
document.write("The
area is "+area(a,b))
</script>
</body>
<html>
Now we would like to implement for you an example, which
only will teach and show you the possibilities of some functions:
The code is written here:
<html>
<head>
<title>FUNCTIONS</title>
</head>
<body>
<script>
function MAIN_EXAMPLE()
{
var x=window.confirm("This
is a question and the continuation is depended of your
answer! So, are you sure you are OK?")
if (x)
window.alert("Good!
This answer is shown because you have clicked the button OK!")
else
window.alert("Too
bad! This answer is shown because you haven't clicked
the button OK!")
alert("This message is shown with a method ALERT of
the object WINDOW!
You can use it many times in your script!")
var y=window.prompt("Now we are using a method PROMPT
of the object
WINDOW! Your answer will be taken as a value of
the parameter Y! So, please, enter your name instead
of the text undefined!")
window.alert("The
result of PROMPT is: "+y)
document.write("This is shown with a method WRITE
of the object DOCUMENT!
It is included in the body of the function!")
}
window.alert("We
are using a method ALERT of the object WINDOW!
This part is not included in a function! Let us start the
execution of the function MAIN_EXAMPLE!")
MAIN_EXAMPLE()
document.writeln("Be
patient! Just now started the text, which isn't included in
the function! We are using again the method WRITE of the
object DOCUMENT!")
</script>
</body>
</html>
|