Practical 6
Practical 6
Aim: - Write program to understand the basic concepts of JavaScript and its dialog boxes.
Tools: Notepad Editor and Web Browser like Internet Explorer, Google Chrome or Mozilla
Firefox.
Theory:
What is JavaScript?
Unlike HTML, JavaScript is case sensitive - therefore watch your capitalization closely when
you write JavaScript statements, create or call variables, objects and functions.
Web Designing and Internet Applications 2
JavaScript Statements
A JavaScript statement is a command to a browser. The purpose of the command is to tell the
browser what to do. This JavaScript statement tells the browser to write "Hello” to the web
page:
document.write("Hello ");
It is normal to add a semicolon at the end of each executable statement. Most people think this
is a good programming practice, and most often you will see this in JavaScript examples on
the web.
The semicolon is optional (according to the JavaScript standard), and the browser is supposed
to interpret the end of the line as the end of the statement. Because of this you will often see
examples without the semicolon at the end.
JavaScript Code
JavaScript code (or just JavaScript) is a sequence of JavaScript statements. Each statement is
executed by the browser in the sequence they are written.
Example:
<html>
<body>
<script type="text/javascript"> document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another
paragraph.</p>");
</script>
</body>
</html>
Event Handler
1. Alert:
<html>
<head>
<form>
<input type="button" onclick="alert('Are you sure you want to give us the deed to your house?')"
value="Confirmation Alert">
</form>
</head>
</html>
2. Confirm
<head>
<script type="text/javascript">
<!--
function confirmation() {
var answer = confirm("really want to leave this website?");
if (answer){
alert("Bye bye user!");
window.open("http://www.google.com/");
}
else{
alert("Thanks for sticking around!");
}
}
</script>
</head>
<body>
<form>
<input type="button" onClick="confirmation()" value="Leave this site?">
</form>
<p >Click on the button to check the confirm dialog box!</p>
</body>
Web Designing and Internet Applications 4
3. Prompt
<html>
<head>
<script>
function myFunctionPro() {
var person = prompt("Please enter your name", "enter name");
if (person == null || person == "") {
txt = "User cancelled the prompt.";
alert(txt);
} else {
txt = "Hello " + person + "! How are you today?";
alert(txt);
}
}
document.write(Date());
</script>
</head>
<body>
<p>Click the button to display an Prompt box.</p>
<button onclick="myFunctionPro()">Try Prompt</button>
</body>
</html>
Conclusion:
The JavaScript code is successfully executed and provided the information to use the syntax,
tag and dialog boxes to the users.