Topic-3 Advanced JavaScript
Topic-3 Advanced JavaScript
Advanced JavaScript
What is Script?
What is JavaScript?
Feature of JavaScript:-
1] JavaScript is light weight scripting language because it does not support all
feature of object oriented programming language.
6] Software that can run on any hardware platform (Mac, PC) and software
platform (windows, Linux) is called platform independent software. Due to this
feature it is called universal scripting language.
Types of Scripting Language :-
JavaScript Comment :-
By adding comment user can add more information about any statement and it
also used to hide information from normal user.
1. Single-line Comment(//)
2. Multi-line Comment(/* ..*/)
JavaScript Variable :-
Document.Write(“msg”) :-
<body>
<p>Click the following button to see the result: </p>
<form>
<input type = "button" value = "Click Me" onclick = "Warn();" />
</form>
</body>
</html>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type = "button" value = "Click Me" onclick = "getConfirmation();" />
</form>
</body>
</html>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type = "button" value = "Click Me" onclick = "getValue();" />
</form>
</body>
</html>
Q. Write JS code to print addition of two no using prompt dialog box and using form
Using Prompt :-
<html>
<head>
</head>
<body>
<script>
var a,b,c;
c=a+b;
document.write("addition is:"+c);
</script>
</body>
</html>
Using form :-
<html>
<body>
<form name="f1">
</form>
<script>
function add()
var a,b,c;
a=parseInt(f1.t1.value);
b=parseInt(f1.t2.value);
c=a+b;
alert("addition is:"+c);
</script>
</body>
</html>
Home Work:-
1] Write JS Code print code to display area (hint area=n*n) accept value from user.
1] If statement:-
If (expression)
//code to execute
2] If..else statement :-
If the condition become true then if block execute and if condition become false
then else block is execute.
if(expression)
else
3] if..else if statement :-
User want to check more than one condition then if..else if statement is used
If(expression)
{
}
else if(expression)
else
JavaScript has a built-in multiway decision statement known as Switch. The switch
statement test the value of given expression with all case. There should not be
duplicity between cases. The value for the case must be similar data type as the
variable in switch. The default statement is not mandatory.
Syntax:-
switch(expression)
Case value1:
Statement block 1;
Break;
Case value 2:
Statement block 2;
Break;
Default :
Statement block;
Looping Statement:-
1] For... Loop:-
This loop executes statement as long as condition become true, control comes out
from the loop when condition becomes false. Benefit of for loop is that it combine
initialization, condition and increment and decrement in single statement.
Syntax:-
Statement block;
Initialization is assigning initial value to the variable, which execute only once then
condition is checked.
Loop will execute statements when condition become true, and when condition
becomes false then control is transferred out of loop.
Syntax:-
Initialization;
While (condition)
Statement block;
The statement within loop may be single line or block of statement. If the
statement within loop is single line then parenthesis is optional. Here loop is
executed repeatedly as long as condition is true. Note that if condition always true
then loop would execute infinitely so after some execution condition become false.
For loop :-
<html>
<head>
<title>Print 1 to 10</title>
</head>
<body>
<script>
var i;
for(i=1;i<=10;i++)
{
document.write("print 1 to 10 no:"+i);
document.write("<br>");
</script>
</body>
</html>
While loop:-
<html>
<head>
<title>Print 1 to 10</title>
</head>
<body>
<script>
var i=1;
while(i<=10)
document.write("no is:"+i);
i++;
document.write("<br>");
</script>
</body>
</html>
Home Work:-
Break statement is used to jump out of loop. It is used to make early exit from
loop. When keyword break is used then control automatically passes to next
statement.
Object in Javascript :-
2] User defined object – new keyword used to create user defined object.
D=new Date ()
When HTML document is loaded into web browser it becomes document object
model. It defines logical structure of document. By using this way HTML
content is accessed and modified called document object model.
The w3c Document object model is platform and language interface that allow
program and script dynamically access and update content, structure and style
document.
1] Window Object:-
innnerHTML property:-
The innerHTML property is useful for getting html element and changing its
content. The innerHTML property can be used to change HTML element
include <html> and <body>.
<html>
<head>
<script type="text/javascript">
functionchangeText()
varclosestyle="</h2>";
document.getElementById('para').innerHTML=style+text+closestyle;
function hello()
alert("hi");
</script></head>
<body style="background-color:cyan">
<h1 align="center">
</h1>
</body></html>
Window Object :-
Property Description
Name Set or return name of window
Location Return location object for window
Document Return document object for window
Status Set or return text in status bar in window
Closed Return Boolean value indicating whether window has been
closed or not
Method Description
alert() Display alert box containing msg with ok button
Confirm() Display alert box containing msg with ok and cancel
button
Prompt() It take input from textbox
Open() Open new window
Close() Close current window
Blur() Remove focus from current window
Focus() Set focus to current window
Print() Print content of current window
setTimeout() Calls function and evaluate expression after specified no of
miliseconds
<!DOCTYPE html>
<html>
<head>
<script>
functionmakeWindow()
varnewwin=window.open();
newwin.document.body.style.backgroundColor="skyblue";
</script>
</head>
<body>
<form>
</form>
</body>
</html>
setTimeout.html
<!DOCTYPE html>
<html>
<head>
<title>setTimeout Function</title>
<script>
functionsamplefunction()
window.setTimeout(next(),4000);
function next()
</script>
</head>
<body style="background-color:cyan">
<h1 align="center">
</h1>
</body>
</html>
JavaScript Event :-
JavaScript has several built in or core language object. These built in object are
available regardless of window content and operates independently of whatever
page browser loaded.
1] String Object: -
String is used to store zero or more character of text within single or double quote.
String is generally used to store and manipulate text.
Property Description
Length Return number of character in a string
Method Description
charAt() Return the character at specified
position
indexOf() Return the index of first occurrence
specified character in given string or -1
specify it never occur
lastindexOf() Return the index of last occurrence of
specified character in given string
Substr(14,7) It return 7 character from 14th character
Substring(14,7) Return all character from 7th to 14th
Trim() Trim() method remove white space from
string
toLowerCase() Convert string into lowercase
toUpperCase() Convert string into uppercase
Number Object :-
It helps to work with number. Primitive value (like 34 or 3.14) cannot have
properties and method, but with JavaScript it is available with primitive value.
Property Description
MIN_VALUE Return the largest minimum value
MAX_VALUE Return the largest maximum value
NaN It represent not a number value
Method Description
isInteger() It return whether the given value is
integer
parseFloat() It convert the given string into floating
frame
parseInt() It convert the given string into integer
number
isFixed() It return that string represent no with
exact digit after decimal point