An Introduction
Introduction
JavaScript is a scripting language most often used for
client-side web development
JavaScript was originally developed by Brendan Eich
of Netscape under the name Mocha, later LiveScript,
and finally renamed to JavaScript
JavaScript was first introduced and deployed in the
Netscape browser version 2.0B3 in December of 1995
JavaScript vs Java
JavaScript and Java has only syntactic relationship
Characterized by many as a marketing ploy by Netscape to
give JavaScript the cachet of what was then the hot new
web-programming language..
Despite the name, JavaScript is unrelated to the
Java programming language
Java (developed by Sun Microsystems) is a powerful and
much more complex programming language
What is Javascript?
JavaScript was designed to add interactivity to HTML pages
JavaScript is a scripting language
A JavaScript consists of lines of executable computer code
A 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
What can a JavaScript Do?
JavaScript gives HTML designers a programming tool
JavaScript can put dynamic text into an HTML page
JavaScript can react to events
JavaScript can read and write HTML elements
JavaScript can be used to validate data
JavaScript can be used to detect the visitor's browser
JavaScript can be used to create cookies
How to insert to Javascript?
The HTML <script> tag is used to insert a JavaScript
into an HTML page.
<html>
<body>
<script type="text/javascript">
document.write("Hello World!")
</script>
</body>
</html>
The Semicolon
Many programmers continue this habit when writing
JavaScript,
But in general, semicolons are optional!
However, semicolons are required if you want to put
more than one statement on a single line.
Handling Older Browsers
Browsers that do not support JavaScript will display the script as page content. To
prevent them from doing this, we may use the HTML comment tag:
<script type="text/javascript">
<!–
document.write("Hello World!")
//-->
</script>
The two forward slashes at the end of comment line (//) are a JavaScript comment
symbol. This prevents the JavaScript compiler from compiling the line.
JavaScript Head Vs Body
JavaScripts in the body section will be executed
WHILE the page loads.
JavaScripts in the head section will be executed when
CALLED.
External JS
Sometimes you might want to run the same
JavaScript on several pages, without having to write
the same script on every page.
To simplify this, you can write a JavaScript in an
external file. Save the external JavaScript file with a .js
file extension.
Note: The external script cannot contain the <script>
tag!
To use the external script, point to the .js file in the
"src" attribute of the <script> tag:
External JS
JavaScript Variables
A variable is a "container" for information you want to
store.
Rules for variable names:
Variable names are case sensitive
They must begin with a letter or the underscore
character
You can create a variable with the var statement:
var strname = some value
You can also create a variable without the var statement:
strname = some value
Operators
As normal to other C Based Langs
JavaScript Popup Boxes
In JavaScript we can create three kinds of popup
boxes
Alert
Alert : An alert box is often used if you want to make
sure information comes through to the user.
alert("sometext")
Confirm
A confirm box is often used if you want the user to
verify or accept something.
When a confirm box pops up, the user will have to
click either "OK" or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the
user clicks "Cancel", the box returns false.
confirm("sometext")
Prompt Box
A prompt box is often used if you want the user to
input a value before entering a page.
When a prompt box pops up, the user will have to
click either "OK" or "Cancel" to proceed after entering
an input value.
If the user clicks "OK" the box returns the input
value. If the user clicks "Cancel" the box returns null.
Syntax:
prompt("sometext","defaultvalue")