| JavaScript Introduction
|
|
"JavaScript is an object-oriented scripting language used to enable programmatic
access to objects within both the client application and other applications (or) Client side Webpage validation."
JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer,
Firefox, Chrome, Opera, and Safari.
Before you continue you should have a basic understanding of the following:
» HTML
If you want to study these subjects first learn HTML.
|
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.
» JavaScript is usually embedded directly into HTML pages.
» JavaScript is an interpreted language (means that scripts execute without preliminary compilation).
» Everyone can use JavaScript without purchasing a license.
|
The example below shows how to use JavaScript to write text on a web page:
Example
<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
|
|
|
The example below shows how to use JavaScript to write text on a web page:
Example
<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
|
|
|
Where to Put the JavaScript
JavaScripts 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, or at a later event, such as when a user clicks a button.
When this is the case we put the script inside a function, you will learn about functions in a later chapter.
Scripts in <head>
Scripts to be executed when they are called, or when an event is triggered, are placed in functions.
|
Put your functions in the head section, this way they are all in one place, and they do not interfere with page content.
Example
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>
<body onload="message()">
</body>
</html>
|
|
|
|
Scripts <body> If you don't want your script to be placed inside a function, or if your script should write page content, it should be placed in the body section.
Example
<html>
<head>
</head>
<body onload="message()">
<script type="text/javascript">
document.write("This message is written by JavaScript");
</script>
</body>
</html>
|
|
|
|
Scripts<head> and <body>
You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.
Example
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>
<body onload="message()">
<script type="text/javascript">
document.write("This message is written by JavaScript");
</script>
</body>
</html>
|
|
|