Java Script
Java Script
Advantages of JavaScript
-> Less server interaction: You can validate user input before sending the page off to the server.
This saves server traffic, which means less load on your server.
-> Immediate feedback to the visitors: They don't have to wait for a page reload to see if they
have forgotten to enter something.
-> Increased interactivity: You can create interfaces that react when the user hovers over them
with a mouse or activates them via the keyboard.
-> Richer interfaces: You can use JavaScript to include such items as drag-anddrop components
and sliders to give a Rich Interface to your site visitors.
Limitations of JavaScript
JavaScript can be implemented using JavaScript statements that are placed within
1. Language: This attribute specifies what scripting language you are using. Typically, its value
will be javascript. Although recent versions of HTML (and XHTML, its successor) have phased
out the use of this attribute.
2. Type: This attribute is what is now recommended to indicate the scripting language in use and
its value should be set to "text/javascript".
Conditional statements
i) If else
ii) switch
Functions in java
A JavaScript function is defined with the function keyword, followed by a name, followed by
parentheses ().
Function names can contain letters, digits, underscores, and dollar signs (same rules as
variables). The parentheses may include parameter names separated by commas: (parameter1,
parameter2, ...)
The code to be executed, by the function, is placed inside curly brackets: {}
var x = myFunction(4, 3); // Function is called, return value will end up in x
function myFunction(a, b)
{
return a * b; // Function returns the product of a and b
}
Pop up Box:
Alert Box
An alert box is often used if you want to make sure information comes through to the user. When
an alert box pops up, the user will have to click "OK" to proceed.
Syntax
window.alert("sometext");
Confirm Box
A confirm box is often used if you want the user to verify or accept something. When a confirm
box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks
"OK", the box returns true. If the user clicks "Cancel", the box returns false.
if (confirm("Press a button!")) {
txt = "You pressed OK!";
}
else {
txt = "You pressed Cancel!";
}
Prompt Box
A prompt box is often used if you want the user to input a value before entering a page. When a
prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering
an input value. If the user clicks "OK" the box returns the input value. If the user clicks "Cancel"
the box returns null.
Syntax
window.prompt("sometext","defaultText");
var person = prompt("Please enter your name", "Harry Potter");
if (person == null || person == "") {
txt = "User cancelled the prompt.";
} else {
txt = "Hello " + person + "! How are you today?";
}
Events
Array
var cars = ["Saab", "Volvo", "BMW"];
<html>
<body>
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
getElementById
The getElementById() method returns the element that has the ID attribute with the specified
value.
This method is one of the most common methods in the HTML DOM, and is used almost every
time you want to manipulate, or get info from, an element on your document. Returns null if no
elements with the specified ID exists.
<html>
<body>
<p id="demo">Click the button to change the text in this paragraph.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</body>
</html>
getElementsByName()
<html>
<body>
First Name: <input name="fname" type="text" value="Michael"><br>
First Name: <input name="fname" type="text" value="Doug">
<p>Click the button to get the tag name of the first element in the document that
has a name attribute with the value "fname".</p>