0% found this document useful (0 votes)
100 views

Javascript Examples

The document provides examples of using JavaScript to display the date and time, an if/else statement, alert boxes, confirm boxes, and prompt boxes. It includes code snippets to demonstrate each JavaScript feature.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Javascript Examples

The document provides examples of using JavaScript to display the date and time, an if/else statement, alert boxes, confirm boxes, and prompt boxes. It includes code snippets to demonstrate each JavaScript feature.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

JAVASCRIPT EXAMPLES

DISPLAY DATE AND TIME

EXAMPLE USING IF..ELSE STATEMENT

<html>
<body>

<html>
<body>

<h1>My First Web Page</h1>

<script type="text/javascript">
var d = new Date();
var time = d.getHours();

<script type="text/javascript">
document.write("<p>" + Date() + "</p>");
</script>
</body>
</html>

if (time < 10)


{
document.write("<b>Good morning</b>");
}
else
{
document.write("<b>Good day</b>");
}
</script>
<p>
This example demonstrates the If...Else statement.
</p>
<p>
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
Otherwise you will get a "Good day" greeting.
</p>

ALERT BOX
<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("Hello! I am an alert box!");
}
</script>
</head>
<body>
<input type="button" onclick="show_alert()" value="Show
alert box" />
</body>
</html>

</body>
</html>
ALERT BOX WITH LINE BREAK
<html>
<head>
<script type="text/javascript">
function disp_alert()
{
alert("Hello again! This is how we" + '\n' + "add line breaks to
an alert box!");
}
</script>
</head>
<body>
<input type="button" onclick="disp_alert()" value="Display
alert box" />
</body>
</html>

CONFIRM BOX
<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button!");
if (r==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
}
</script>
</head>
<body>
<input type="button" onclick="show_confirm()" value="Show
a confirm box" />
</body>
</html>

PROMPT BOX
<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var name=prompt("Please enter your name","Harry Potter");
if (name!=null && name!="")
{
document.write("Hello " + name + "! How are you today?");
}
}
</script>
</head>
<body>
<input type="button" onclick="show_prompt()" value="Show
prompt box" />
</body>
</html>

You might also like