(JavaScript Lab)
Lab # 8
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)
Start End Tag:
<script type="text/javascript">
</script>
JavaScript is Case Sensitive
Unlike HTML, JavaScript is case sensitive-therefore watch your capitalization closely when you
write JavaScript statements, create or call variables, objects and functions.
JavaScript Statements
A JavaScript statement is a command to a browser. The purpose of thecommand is to tell the
browser what to do.
This JavaScript statement tells the browser to write "Hello " to the web page:
document.write("Hello ");
Alert Box
Code 1
<!DOCTYPE html>
<html>
<head>
<script>
background-color:orange;
display:inline-block;
padding:9px;}
#header
{
background-color:red;
height:60px;}
</style>
</head>
<body >
<div id="header">
<a href="#" >link1</a>
<a href="#" >link1</a>
<a href="#" >link1</a>
</div>
</body>
</html>
Code 2
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
alert("I am an alert box!");
}
</script>
</head>
<body>
<p>Click the button to display an alert box:</p>
<button onclick="myFunction()">Try it</button>
</body>
</html>
Confirm Box
CODE 1
<!DOCTYPE html>
<html>
<head>
<script>
confirm("Its JavaScript Lab");
</script>
<style>
a{
background-color:orange;
display:inline-block;
padding:9px;}
#header
{
background-color:red;
height:60px;}
</style>
</head>
<body >
<div id="header">
<a href="#" >link1</a>
<a href="#" >link1</a>
<a href="#" >link1</a>
</div>
</body>
</html>
CODE 2
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display a confirm box.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x;
if (confirm("Press a button!") == true) {
x = "You pressed OK!";
} else {
x = "You pressed Cancel!";
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Prompt Box
<!DOCTYPE html>
<html>
<body>
<p>Click the button to demonstrate the prompt box.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.getElementById("demo").innerHTML =
"Hello " + person + "! How are you today?";
}
</script>
</body>
</html>
<html>
<head>
<script>
function change()
{ document.getElementById("demo").innerHTML = "Hello ";
}
</script>
</head>
<body>
<p id="demo">JavaScript can change HTML content.</p>
<button type="button" onclick= "change()"> Click Me!</button>
</body>
</html>
</head>
<body>
<p id="demo">JavaScript can change the style of an HTML element. </p>
<button type="button" onclick="myFunction()">Click Me! </button>
</body>
</html>
<html>
<head>
<script>
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == null || x == "") {
alert("Name must be filled out");
return false;
}
}
</script>
</head>
<body>
</body>
</html>
<head>
<title>onClick Event Handler Example</title>
<script type="text/javascript">
function warnUser() {
return confirm("Are you a student?");
}
</script>
</head>
<body>
<a href="ref.html" onClick="return warnUser()"> Students access only</a>
</body>
</html>
</body>
</html>
Onkeyup Event
<!DOCTYPE html>
<html>
<body>
<p>A function is triggered when the user releases a key in the input field. The function transforms the
character to upper case.</p>
Enter your name: <input type="text" id="fname" onkeyup="myFunction()">
<script>
function myFunction() {
var x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
LAB TASK:
1)
2)
3)