The document provides examples of JavaScript code for basic programs including using alert, confirm, and prompt boxes, functions, event handlers, manipulating styles, displaying dates, and opening new windows. It also shows how to integrate JavaScript with HTML and CSS to manipulate the DOM, display images, and change styles on user interactions like clicks and hovers.
5. Variables and Conditions
<script>
var x=window.confirm("Are you sure you want
to quit")
if (x)
window.alert("Thank you.")
else
window.alert("Good choice.")
</script>
6. <html><head>
<script>
var x=confirm("Are you sure you want to quit?")
if (!x)
window.location="http://www.yahoo.com"
</script>
</head>
<body>
Welcome to my website!.
</body></html>
10. <!DOCTYPE html>
<html>
<body>
<h1>My First JavaScript</h1>
<p id="demo">JavaScript can change the style of an HTML element.</p>
<script>
function myFunction() {
document.getElementById("demo").style.fontSize = "25px";
document.getElementById("demo").style.color = "red";
document.getElementById("demo").style.backgroundColor = "yellow";
}
</script>
<button type="button" onclick="myFunction()">Click Me!</button>
</body>
</html>
11. Event handler
What are event handlers?
execute JavaScript when
something happens, such as
• click or move your mouse over
a link,
• submit a form etc.
15. <html><head><title>show
date</title></head>
<body>
<script language="javascript">
var bantime= new date()
var ss=bantime.gethours()
if (ss<=20)
document.write("<img src='http://www.world-trail.com/wp-
content/uploads/2015/08/world_trail_logo.png'>")
else
document.write("<img
src='https://static01.nyt.com/images/2016/05/03/world/what-in-
the-world/witw-promo/witw-promo-thumblarge.jpg'>")
</script>
</body></html>
16.
17. Window To open a window, simply use the method
"window.open()":
<html><head><title>new
window</title></head>
<body>
<form>
<input type="button" value="click
here to see"
onclick="window.open('https://w
ww.youtube.com/?gl=in')">
</form>
</body></html>
23. function showDate(){
var time = document.getElementById("time");
time.innerHTML = Date();
}
function clearDate(){
var time = document.getElementById("time");
time.innerHTML = '';
}
Javascript