Javascript
Javascript
What is JavaScript ?
JavaScript
is:
JavaScript is a lightweight,
interpreted programming
language
Complementary to and
integrated with Java
Complementary to and
integrated with HTML
Open and cross-platform
Advantages of JavaScript:
The
Include <script>
There
If
Example
<html> <head>
<script type="text/javascript">
<!-- function sayHello() {
alert("Hello World") } //-->
</script> </head>
<body> <input type="button"
onclick="sayHello()" value="Say
Hello" /> </body> </html
If
Example:
<html>
<head>
</head>
<body> <script
type="text/javascript">
<!
document.write("Hello World")
//--> </script> <p>This is web
page body </p> </body>
</html>
JavaScript DataTypes:
JavaScript
Example:
<script type="text/javascript">
<!
var age = 20;
if( age > 18 )
{ document.write("<b>Qualifies
for driving</b>"); }
//--> </script>
Switch case
<script type="text/javascript">
var grade='A';
document.write("Entering switch block<br />");
switch (grade) {
case 'A': document.write("Good job<br />");
break;
case 'B': document.write("Pretty good<br />");
break;
case 'C': document.write("Passed<br />");
break; case 'D':
document.write("Not so good<br />");
break; case 'F': document.write("Failed<br />");
break;
default: document.write("Unknown grade<br />") } d
Javascript if-else
<html>
<head>
<script
type="text/javascript">
var age=23;
if(age>18)
{
document.write("adult");
}
</script>
</head>
</html>
.innerHTML = x
Example
<html>
<body>
<p>Click the button to calculate x.</p>
<button onclick="myFunction()">Try it</button>
<br/>
<br/>Enter first number:
<input type="text" id="txt1" name="text1">
<br>
Enter second number:
<input type="text" id="txt2" name="text2">
<p id="demo"></p>
<script>
function myFunction() {
var y = document.getElementById("txt1").value;
var z = document.getElementById("txt2").value;
var x = Number(y) + Number(z);
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Date ()
<html>
<head>
<script>
function startTime() {
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
m = checkTime(m);
s = checkTime(s);
h+":"+m+":"+s;
document.getElementById('txt').innerHTML
var t = setTimeout(function(){startTime()},500);
function checkTime(i) {
return i;
</script>
Continue..
JavaScript
..\internet
technology\regis.html
Design a calculator
Requirements
<div
align="center">
<table width="30%"
border="4"> <tr>
<td
colspan="2"align="center">Displ
ay Area</td></tr>
<tr><td>Numeric Buttons</td>
<td>Function Buttons</td>
</tr></table></div>
<input
type="button">
Which gives us a general-purpose
button. To each of these buttons
we'll attach a:
OnClick="DoSomething()"
'event handler', so that when we
click on the button, we can
expect a response.
<FORM name="Calculator">
<table border="4">
output
..\internet
.html
technology\calculator1
Email validation
..\internet
technology\email.html
Javascript Array
An
<p id="demo"></p>
<script>
var cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").i
nnerHTML = cars;
</script>
Push operation
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
document.getElementById("demo").innerHTML = fruits;
</script>
</body>
</html>
Syntax
/pattern/modifiers;
Example
var
patt = /w3schools/i
i Perform case-insensitive matching
g Perform a global match (find all
matches rather than stopping after
the first match)
m Perform multiline matching
[abc]
[^abc]
[0-9]
[^0-9]
Example
var
Output:
<html>
<body>
<p>Replace "microsoft" with "W3Schools" in the
paragraph below:</p>
<button onclick="myFunction()">Try it</button>
<p id="demo">Please visit Microsoft!</p>
<script>
function myFunction() {
var str =
document.getElementById("demo").innerHTML;
var txt = str.replace(/microsoft/i,"W3Schools");
document.getElementById("demo").innerHTML
= txt;
}
Replace "microsoft" with "W3Scho
</script>
in the paragraph below:
</body>
Please visit Microsoft!
</html>
<html>
<body>
<script>
function checkpostal()
{
var re5digit=/^\d{5}$/
if
(document.myform.myinput.value.search(re5digit)=
=-1)
alert("Please enter a valid 5 digit number
inside form")
}
</script>
<form name="myform">
<input type="text" name="myinput" size=15>
<input type="button" onClick="checkpostal()"
value="check">
</form>
</body>
var re5digit=/^\d{5}$/
^
<html>
<body>
<p>Click the button to perfom a global search for the
letters "ain" in a string, and display the matches.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "The rain in SPAIN stays mainly in the plain";
var res = str.match(/ain/g);
document.getElementById("demo").innerHTML = res;
}
Click the button to perform a global
</script>
search for the letters "ain" in a string,
</body>
and display the matches.
</html>
ain,ain,ain
Example
Perform a global, case-insensitive search for "ain":
var
Output:
ain,AIN,ain,ain
var
myRegExp = /\$\d?\d\.\d\d/;
var myText = " The hot dog cost
$1.75!";
This expression will match the dollar sign,
followed by one or two digits, followed by
the decimal point (i.e., period), followed
by two digits. From Table 2, you can see
the "?" character means match 0 or 1 of
the preceding item. In this expression, it
means match zero or one digits.