Introduction to javascript
1. What is JavaScript?
- JavaScript was designed to add interactivity to HTML pages
- JavaScript is a scripting language - a scripting language is a lightweight programming language
- A JavaScript is lines of executable computer code
- A JavaScript is usually embedded directly in HTML pages
- JavaScript is an interpreted language (means that scripts execute without preliminary compilation)
- Everyone can use JavaScript without purchasing a license
- JavaScript is supported by all major browsers, like Netscape and Internet Explorer
2. How to Put a JavaScript Into an HTML Page
<html>
<body>
<script type="text/javascript">
document.write("Hello World!")</script>
</body>
</html>
3.Where to Put the JavaScript?
Scripts in a page will be executed immediately while the page
loads into the browser. This is not always what we want. Sometimes we want to
execute a script when a page loads, other times when a user triggers an event.
Scripts in the head section:
Scripts to be executed when they are called, or when an event is
triggered, go in the head section. When you place a script in the head section,
you will ensure that the script is loaded before anyone uses it.
<html>
<head>
<script type="text/javascript">
some statements
</script>
</head>
Scripts in the body section:
Scripts to be executed when the page loads go in the body section.
When you place a script in the body section it generates the content of the
page.
<html>
<head>
</head>
<body>
<script type="text/javascript">
some statements
</script>
</body>
Scripts in both the body and the head section:
You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.
<html>
<head>
<script type="text/javascript">
some statements
</script>
</head>
<body>
<script type="text/javascript">
some statements
</script>
</body>
13. How to Define a Function
To create a
function you define its name, any values ("arguments"), and some
statements:
function myfunction(argument1,argument2,etc) {some statements } |
A function with no arguments must include the parentheses:
function myfunction() {some statements } |
Arguments
are variables used in the function. The variable values are values passed on by
the function call.
By placing
functions in the head section of the document, you make sure that all the code
in the function has been loaded before the function is called.
Some
functions return a value to the calling expression
function result(a,b) {c=a+b return c } |
14. How to Call a Function?
A function
is not executed before it is called.
You can call
a function containing arguments:
myfunction(argument1,argument2,etc) |
or without arguments:
The return Statement
Functions
that will return a result must use the "return" statement. This
statement specifies the value which will be returned to where the function was
called from. Say you have a function that returns the sum of two numbers:
function total(a,b) {result=a+b return result } |
When you
call this function you must send two arguments with it:
The returned
value from the function (5) will be stored in the variable called sum.