Java Script: An Introduction
Java Script: An Introduction
AN INTRODUCTION
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
Introduction to Java Script
It Introduce Client-Side scripting which makes web page
more dynamic and interactive.
Alter a web page in response to user actions.
React to user events.
Capturing user inputs is typically done through a form –
need for client side form validation.
JavaScript is not Java.
– Developed by Netscape, not Sun.
– Only executed in a browser.
– Is not a full-featured programming language.
– However, the syntax is similar.
Unlike Java, which needs compilation; JavaScript is
dynamic and is interpreted in run-time.
Java script characteristics
Interpreted language
Can be embedded in HTML file or can be
included as an external file
Easy to learn
Quick development
Designed for simple, small programs
Performance
Procedural capabilities
Event handling
Debugging support
Platform independence/Architecture neutrality
Differences (JavaScript is not java)
Java was developed by Sun Microsystems while JavaScript was
developed by Netscape.
Example
<script type="text/javascript">
document.write("<h1>Printing Line with h1 tag inside js</h1>");
</script>
Can insert special characters (like " ' ; &) with the backslash
<script type="text/javascript">
document.write("<h1 style= \"color:red\"> Printing line with red color
inside js </h1>");
</script>
Windows dialog boxes using <alert>
Dialogs typically display important messages to users
browsing the web page.
It pop-up on the screen to grab the users attention.
A predefined dialog <alert> called alert dialog is used for
dialog boxes.
alert is a method of windows object.
Example
<script type="text/javascript">
alert("Hello Java Script");
document.write("Welcome to JavaScript World");
</script>
Note : Dialogs displays plain text
They do not render XHTML
Windows dialog boxes using confirm
box
The confirm box is a box that pops up with both an
OK and a Cancel button. The confirm box is used
to verify acceptance from the user. If the user
accepts, then the user presses the OK button and
the confirm box returns with a true value. If the
user rejects with the Cancel button, then the
confirm box returns false value.
General syntax for a confirm box is
confirm (“textmessage”)
Example for confirm box
<html> The confirm box pops up with the
message:
<body>
<script type="text/javascript"> Wish to accept or Cancel
if (confirm("Wish to accept or Cancel"))
{ Showing two buttons (OK and Cancel)
alert ("True value returned") that the user can choose from. If the
} user presses OK in the confirm box then
the value returned would be true,
else
executing the if block of statements. This
{ results in the alert box popping up with
alert ("False value returned") the message.
}
</script> True value returned.
</body>
</html> If the user presses the Cancel button in
the confirm box then the value returned
would be false, executing the else block
of statements. This results in the alert
box popping up with the message