Hy Is Javascripts Used?
Hy Is Javascripts Used?
c
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
JavaScript is used in millions of Web pages to improve the design, validate forms, detect
browsers, create cookies, and much more.JavaScript is the most popular scripting language
on the internet, and works in all major browsers, such as Internet Explorer, Firefox, and
Opera.
R JavaScript gives HTML designers a programming tool - HTML authors are normally not
programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone can
put small "snippets" of code into their HTML pages
R JavaScript can put dynamic text into an HTML page - A JavaScript statement like this:
document.write("<h1>" + name + "</h1>") can write a variable text into an HTML page
R JavaScript can react to events - A JavaScript can be set to execute when something happens,
like when a page has finished loading or when a user clicks on an HTML element
R JavaScript can read and write HTML elements - A JavaScript can read and change the content
of an HTML element
R JavaScript can be used to validate data - A JavaScript can be used to validate form data before
it is submitted to a server. This saves the server from extra processing
R JavaScript can be used to detect the visitor's browser - A JavaScript can be used to detect the
visitor's browser, and - depending on the browser - load another page specifically designed for that
browser
R JavaScript can be used to create cookies - A JavaScript can be used to store and retrieve
information on the visitor's computer
Ô Ô
c
Ô
<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
The word document.write is a standard JavaScript command for writing output to a page. By entering the
document.write command between the <script> and </script> tags, the browser will recognize it as a
JavaScript command and execute the code line.
JavaScripts in the body section will be executed HILE the page loads.
External script;
<html>
<head>
</head>
<body>
<script src="xxx.js">
</script>
<p>
The actual script is in an external script file called "xxx.js".
</p>
</body>
</html>
Head script
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>
<body onload="message()">
Body section
</body>
</html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("This message is written by JavaScript");
</script>
</body>
</html>
JavaScript Statements
A JavaScript statement is a command to the browser. The purpose of the command is to tell the browser
what to do.
JavaScript Blocks
Blocks start with a left curly bracket {, and ends with a right curly bracket }.
<script type="text/javascript">
{
document.write("<h1>This is a header</h1>");
document.write("<p>This is a paragraph</p>");
document.write("<p>This is another paragraph</p>");
}
</script>
<script type="text/javascript">
//Write a "Good morning" greeting if
//the time is less than 10
var d=new Date();
var time=d.getHours();
if (time<10)
{
document.write("<b>Good morning</b>");
}
</script>
A
In JavaScript we can create three kinds of popup boxes: Alert box, Confirm box, and Prompt
box
Arompt box
<html>
<head>
<script type="text/javascript">
function disp_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>
</body> </html>
Confirm box
<html>
<head>
<script type="text/javascript">
function disp_confirm()
{
var r=confirm("Press a button");
if (r==true)
{
document.write("You pressed OK!");
}
else
{
document.write("You pressed Cancel!");
}
}
</script>
</head>
<body>
</body>
</html>
Alert box
<html>
<head>
<script type="text/javascript">
function disp_alert()
{
alert("I am an alert box!!");
}
</script>
</head>
<body>
</body> </html>
All the functions declarations are in the head..functions are later called in the body