Chapter 4
Chapter 4
Chapter 4
JavaScript code
</script>
Cont…
Example:
<html>
<body>
<script language=“javaScript" type="text/javasScipt">
<!--
document.write("Hello World!")
//-->
</script>
</body>
</html>
Cont…
The HTML DOM (Document Object Model) is the
official W3C standard for accessing HTML elements.
It is very common to use JavaScript to manipulate the
DOM (to change the content of HTML elements).
Example
x = document.getElementById("demo");
Where?
You can write java script code either in heading part or body
part.
Before writing java script code you have to open <script> tag
and close it at the end.
You can place any number of scripts in an HTML document.
And those can be both in <head> and <body>.
Cont…
But it is recommended to keep all your java script code (in a single
page) together.
Java script code can be found alone as external file saved with
extension file .js.
If your code is found externally then you have to call (link) it with
the current page like:
<script src="myScript.js"></script>
You are not expected to use <script> on external java script code.
Java script is case sensitive (lower and upper case letters have
different meaning)
Java script code can be used either in head or body part.
Use semicolon at the end of single line code.
Cont…
Why do we need scripting language in our web pages?
To create
Dynamic
Interactive and
concrete web pages
Creates pages dynamically .
Interact with the user. It can do some processing of forms
and can validate user input when the user submits the
form.
JavaScript Can Change HTML Content.
One of many HTML methods is document.getElementById().
Example:
document.getElementById("head1").innerHTML="Javascript";
Cont…
JavaScript Can Change HTML Styles (CSS)
Changing the style of an HTML element, is a variant of changing an
HTML attribute:
Example:
document.getElementById("head2").style.fontSize="100px";
JS comment
JavaScript comments can be used to explain JavaScript code,
just like other language's comment, text under comment will
not be executed.
So that it makes your code more readable and prevent
execution, when testing alternative code.
Single line and multi line comment are used.
Cont…
Single Line Comments
Single line comments start with //. Any text starting with // up
to the end of the line will be ignored by JavaScript (will not be
executed).
Multi-line Comments
Multi-line comments start with /* and end with */.
Any text between /* and */ will be ignored by JavaScript.
<script>
Example: //document.write("My Script");
document.write("Second line");
/*document.write("third line"); Output
document.write("fourth line");
*/
</script>
Cont…
From the above example:
The first line is commented using single line comment.
The third and fourth lines are commented using multi line
comment, which prevents all of them from being executed.
So second line is the only to be displayed.
To display output
Java script can display output in different ways.
"innerHTML“
"document.write()" and
"alert" are used to display output.
Cont…
Example:-1
<script>
alert("Text with Box");
</script>
On the above code <h1> has no element and it has id with the
name "try".
In the java script code, document.getElementById() is used to
access any HTML element with its id.
Cont…
And we use the id "try" to access <h1>. innerHTML is used to
manage the content of tag with that id. And "text in heading" is
assigned for <h1> and it is displayed.
Example-4:
<body>
<h1 id="try">this is heading text</h1>
<script>
var x;
x=document.getElementById("try").innerHTML;
</script>
</body>
In this case, x has value "this is heading text" because we assigned
the content of id called "try" to a variable x.
Cont…
We can use java script
When the body is loaded
Example:
<html><head> <title></title>
<script type=“text/java Script”>
<!--
function name()
{
document.write(“her name is A”);
}
//-->
</script>
</head>
<body onload=“name()”>
</body></html>
Cont…
Following user actions(Events)
Example:
<html>
<head>
<script type="text/javaScript">
<!--
function openwindow()
{
window.open(“Form1.html");
}
//-->
</script>
</head>
<body>
<form
<input type="submit" value="open window" onClick="openwindow()">
</form>
</body></html>
Form Validation : Example
<html> <head>
if(submitOk="false")
<script type="text/javaScript">
{ return false;
function validate()
{
}
var u=document.getElementById("user").value; }
var pw=document.getElementById("pass").value; </script>
var e=document.getElementById("email").value.indexOf("@"); </head>
submitOk="true";
<body>
if(u!="Ambo")
<form onsubmit="return validate()"
{
action="">
alert("Invalid user name");
submitOk="false"; User:<input type="text" id="user"
} size="20"><br>
if(pw!="well" || pw=="") PW: <input type="text" id="pass"
{
size="20"><br>
alert("invalid pass word");
Email:<input type="text" id="email"
submitOk="false";
size="20"><br>
}
if(e==-1){
<input type="submit"
alert("Invalid email"); value="open">
submitOk="false"; </form>
}
</body></html>
Example: Cont…
<html> <script>
<head> function try1()
</head> {
<body> var x=document.info.name.value;
<form name=“info”> if(x=="")
<p> Name <input type="text" alert("please enter value");
name="name"></p>
}
<p> ID <input type="text"
name="id"></p> </script>
<p><input type="submit" </body>
name="button" value="Go"
</html>
onClick="try1()"></p>
</form>
Open and Close Windows
<html>
<head>
<script type=“text/javascript”>
function open()
{
window.open(“Form1.html” );
}
</script>
</head>
<body>
<form>
<input type=“button” value=“open window” onClick=“open()”>
</form>
</body>
</html>
Cont…
Confirm Box: used if you want the user to verify or accept something.
<html><head>
<script type="text/javaScript">
<!--
function confirwin()
{
var x=confirm("press ok button to open the window");
if(x==true)
{
window.open("Form1.html");
}
else{
window.close("Form1.html");
} }
//-->
</script></head>
<body>
<form>
<input type="button" value="open window" onClick="confirwin()">
</form></body></html>
Prompt Box:
<html><head>
<script type="text/javaScript">
<!--
function promwin()
{
var x=prompt("enter your name");
if(x=="AA")
{
window.open("Form1.html");
}
else{
window.close("Form1.html");
}}
//-->
</script></head>
<body><form>
<input type="button" value="open window" onClick="promwin()">
</form></body></html>
Control Statements
Switch statement: used to compare one value to many others
<html><body>
<script type="text/javaScript">
<!--
var x=window.prompt(" write your name pleace");
switch(x) {
case "AA":
document.write("miss you");
document.write("<br>");
break;
case "BB":
document.write("wel go");
document.write("<br>");
break;
default:
document.write("wel come");
}
//-->
</script></body></html>
JavaScript Loops
In programming, loop is used to do activities
repeatedly.
While you interpret the real world to the computer,
activities which are done repeatedly will be managed
by the use of loop.
It will help us to iterate things as long as we want them.