Professional Documents
Culture Documents
"Network-Centric Applications Are Applications That Send Data To The Server and Are Interconnected by
"Network-Centric Applications Are Applications That Send Data To The Server and Are Interconnected by
• 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-and-
drop components and sliders to give a Rich Interface to your site visitors.
Limitations of JavaScript:
• Client-side JavaScript does not allow the reading or writing of files. This has been
kept for security reason.
1] Language
2] Type
<script language = "javaScript" type = "text/javaScript">
JavaScript code
</script>
<html>
<body>
<script language = "javascript" type = "text/javascript">
document.write("Hello World!")
</script>
</body>
</html>
JavaScript Display Possibilities: JavaScript can "display" data in different
ways:
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6; //To access an HTML element,
JavaScript can use the document.getElementById(id) method. The id attribute defines the HTML
element. The innerHTML property defines the HTML content
document.write(5 + 6);//For testing purposes
<button type="button" onclick="document.write(5 + 6)">Try it</button>
window.alert(5 + 6);// window is keyword ,it is optional
alert(5 + 6);//You can use an alert box to display data
console.log(5 + 6);//you can call the console.log() method in the browser to display data.
</script>
</body></html>
JavaScript Data types & Variables
Loop-
i) for
ii) while
iii) do-while
<html>
<body>
<script type = "text/javascript">
<!--
var x = 1;
document.write("Entering the loop<br /> ");
while (x < 20) {
if (x == 5) {
break; // breaks out of loop completely
}
x = x + 1;
document.write( x + "<br />");
}
document.write("Exiting the loop!<br /> ");
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body> </html>
Functions in JavaScript-
function functionname (parameter1, para2…..)
{
// function body
}
Ex:
function showMessage ()
{
alert(‘hello everyone!’)
}
Local variables: A variable declared inside a function is only visible
inside that function.
<script>
function showMessage()
{
message = "Hello, I'm JavaScript!"; // local variable
alert( message );
}
showMessage(); // Hello, I'm JavaScript!
alert( message ); // <-- Error! The variable is local to the function
</script>
Global variables: Variables declared outside of any function ,such as the
outer, username in the code above are called global.
<script>
userName = 'John';
function showMessage()
{
message = 'Hello, ' + userName;
alert(message);
}
showMessage(); // Hello, John
</script>
The return Statement
If you want to return a value from a function.
return value;
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Math</h1>
<h2>The Math.random() Method</h2>
<p>Math.random()*10 returns a random number between 0 and 10:</p>
<p id="demo"></p>
<script>
let x = Math.random() * 10;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Generates alphanumeric OTP of lenght 6:
<script>
function generateOTP() {
var string =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY
Z';
var OTP = ‘ ';
var len = string.length;
for (var i = 0; i < 6; i++ ) {
OTP += string[Math.floor(Math.random() * len)]; }
return OTP; }
document.write("OTP of 6 lenght: ")
document.write( generateOTP() );
</script>
Event? JavaScript's interaction with HTML is handled through events that occur when the user or the browser
manipulates a page.
validate() function returns true, the form will be submitted, otherwise it will not
submit the data.
<script type = "text/javascript">
function validation() {
all validation goes here
.........
return either true or false
} </script>
<body>
<form method = "POST" action = "t.cgi" onsubmit = "return validate()">
<input type = "submit" value = "Submit" />
</form></body></html>
onmouseover and onmouseout-
<script type = "text/javascript">
function over() {
document.write ("Mouse Over");
}
function out() {
document.write ("Mouse Out");
}
</script>
<body>
<p>Bring your mouse inside the division to see the result:</p>
<div onmouseover = "over()" onmouseout = "out()">
<h2> This is inside the division </h2>
</div>
</body>
JavaScript - Errors & Exceptions Handling
For example, the following line causes a runtime error because here the
syntax is correct, but at runtime, it is trying to call a method that does
not exist.
3) Logical Errors -try...catch...finally
<script type = "text/javascript">
try {
// Code to run
[break;]
}
catch ( e ) {
// Code to run if an exception occurs
[break;]
}
[ finally {
// Code that is always executed regardless of
// an exception occurring
}]
</script>
<html> <head><script type = "text/javascript">
function myFunc() {
var a = 100;
try {
alert("Value of variable a is : " + a );}
catch ( e ) {
alert("Error: " + e.description ); }}
</script></head> <body>
<p>Click the following to see the result:</p>
<form>
<input type = "button" value = "Click Me" onclick = "myFunc();" />
</form>
</body>
</html>
<html><head> <script type = "text/javascript">
function myFunc() {
var a = 100;
try {
alert("Value of variable a is : " + a ); }
catch ( e ) {
alert("Error: " + e.description ); }
finally {
alert("Finally block will always execute!"}}
</script> </head><body>
<p>Click the following to see the result:</p>
<form>
<input type = "button" value = "Click Me" onclick = "myFunc();" />
</form></body></html>
Array
Declaration of an Array-basically two ways to declare an array.
1] var house = [ ];
2] var house = new Array( );
Initialization of an Array-
1] var house = [‘1BHK’, ‘2BHK’, ‘3BHK’ ];
house[0] = 1BHK;
house[1] = 2BHK;
Method Description
• In this example, we are going to get the value of input text by user. Here, we are
using document.form1.name.value to get the value of name field.
• Here, document is the root element that represents the html document.
• form1 is the name of the form.
• name is the attribute name of the input text.
• value is the property, that returns the value of the input text.
<script type="text/javascript">
function printvalue(){
var name=document.form1.name.value;
alert("Welcome: "+name); } </script>
<form name="form1">
Enter Name:<input type="text" name="name"/>
<input type="button" onclick="printvalue()" value="print name"/> </form>
Javascript - document.getElementById() method
<script type="text/javascript">
function totalelements()
{
var allgenders=document.getElementsByName("gender");
alert("Total Genders:"+allgenders.length);
} </script>
<form>
Male:<input type="radio" name="gender" value="male">
Female:<input type="radio" name="gender" value="female">
<input type="button" onclick="totalelements()" value="Total Genders">
</form>
document.getElementsByTagName() method
The innerHTML property can be used to write the dynamic html on the html document.
<html><body>
<script type="text/javascript" >
function showcommentform() {
var data="Name:<br><input type='text' name='name'><br>Comment:<br><textarea
rows='5' cols='50'></textarea><br><input type='submit' value='comment'>";
document.getElementById('mylocation').innerHTML=data; } </script>
<form name="myForm">
<input type="button" value="comment" onclick="showcommentform()">
<div id="mylocation"></div>
</form>
</body></html>
Show/Hide Comment Form Example using innerHTML
<script>
var flag=true;
function commentform(){
var cform="<form action='Comment'>Enter Name:<br><input type='text' name='name'/><br/>
Enter Email:<br><input type='email' name='email'/><br>Enter Comment:<br/>
<textarea rows='5' cols='70'></textarea><br><input type='submit' value='Post
Comment'/></form>";
if(flag){
document.getElementById("mylocation").innerHTML=cform;
flag=false;
}else{
document.getElementById("mylocation").innerHTML="";
flag=true; } }
</script> </head> <body>
<button onclick="commentform()">Comment</button>
innerText
• The innerText property can be used to write the dynamic text on the
html document. Here, text will not be interpreted as html text but a
normal text.
• It is used mostly in the web pages to generate the dynamic content
such as writing the validation message, password strength etc.
<html><body><script type="text/javascript" >
function validate() {
var msg;
if(document.myForm.userPass.value.length>5){
msg="good";
}
else{
msg="poor"; }
document.getElementById('mylocation').innerText=msg; }
</script>
<form name="myForm">
<input type="password" value="" name="userPass" onkeyup="validate()">
Strength:<span id="mylocation">no strength</span>
</form> </body></html>
<html><body><script>-VALIDATION
function validateform(){
var name=document.myform.name.value;
var password=document.myform.password.value;
if (name==null || name==""){
alert("Name can't be blank");
return false;
}else if(password.length<6){
alert("Password must be at least 6 characters long.");
return false;
} } </script> <body>
<form name="myform" method="post" action="http://www.google.com/javascriptpages/valid.jsp"
onsubmit="return validateform()" >
Name: <input type="text" name="name"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="register"> </form> </body></html>
Retype Password Validation
The this keyword is a reference variable that refers to the current object.
<script>
var address=
{
company:"Javascript",
city:"Noida",
state:"UP",
fullAddress:function()
{
return this.company+" "+this.city+" "+this.state;
} };
var fetch=address.fullAddress();
document.writeln(fetch);
</script>
Global Context
In global context, variables are declared outside the function. Here, this
keyword refers to the window object.
<script>
var website=“yahoo";
function web()
{
document.write(this.website);
}
web();
</script>
The call() and apply() method? Do it by yourself
<script>
var emp_address = {
fullAddress: function() {
return this.company + " " + this.city+" "+this.state;
}
}
var address = {
company:"Javascript",
city:"Noida",
state:"UP", }
document.writeln(emp_address.fullAddress.call(address));
</script>