Java Script Intro
Java Script Intro
2
Amity School of Engineering & Technology
Introduction to Scripting
• Scripting Languages are mainly used to build the programming environment in
HTML document
• Pages will be made more dynamic and interactive.
• Some languages : VBScript, JavaScript, Jscript and ECMA Script
• Browser Includes Scripting Interpreter (Technique uses by attacker as well)
• Choosing a Scripting Language
– Browser compatibility
– Programmer familiarity
• Client or server can execute the scripts.
3
Amity School of Engineering & Technology
4
Amity School of Engineering & Technology
Sample JS screen
5
Amity School of Engineering & Technology
6
Amity School of Engineering & Technology
Features of JavaScript
7
Amity School of Engineering & Technology
8
Amity School of Engineering & Technology
<SCRIPT Type=“Text/JavaScript”>
-----
(JavaScript code goes here)
----
</SCRIPT>
9
Amity School of Engineering & Technology
10
Amity School of Engineering & Technology
<script language="JavaScript">
<!--
/*calling function when user clicks on the button */
function msg(){
alert("Hi");
}
// -->
</script>
<form name="f1">
<input type="button" value=" ok "
onClick="msg()">
</form>
11
Amity School of Engineering & Technology
JavaScript –Variables
• Declared using the keyword “var”. Declaring variables is not mandatory.
• Must start with a letter or an underscore and can have digits.
• Does not have explicit data types.
• The Data type is automatically decided by the usage.
• Scope is by default global. If a variable is prefixed by the keyword “var” within a
function then it is a local variable.
• The formal parameters are local to the function.
function demo()
{ var inum1 = 10; // Local to the function
inum2 = 20; // Global to the document.
}
demo(); // Invoking function
inum1 = inum1+1; // Error because inum1 is local variable
inum2 = inum2+1; // no Error
12
Amity School of Engineering & Technology
JavaScript – Operators
• Arithmetic Operators
+, ++, -, --, *, /, %
• Relational Operators
==, !=, ===, !==, >, >=, < , <= strict.html
• Logical Operators ( and , or , not)
&&, ||, !
• Assignment Operators
=, +=, -=, *=, /=, %=
• Strict equal (===)
Returns true if the operands are equal and of the same type.
• Strict not equal (!==)
Returns true if the operands are not equal and/or not of the same type.
14
Amity School of Engineering & Technology
Special operators
• typeof operator
– Unary operator
– Indicates the data type of the operand.
Eg:
x=123;
alert(typeof(x)); // Number
x="Hello"
alert(typeof(x)); // String
• new
– Used for instantiation of objects.
Eg: today = new Date( )
• this
– used to refer to the current object
16
Amity School of Engineering & Technology
17
Amity School of Engineering & Technology
control.html (control.html.HTM)
18
Amity School of Engineering & Technology
JavaScript – Loop
• while loop
iterates through a block of statements until the condition becomes false
Syntax : while ( test condition) {
zero or more statements
}
• for loop
Iterate through a block of statements for based on the range of values
Syntax : for(initstmt; condstmt; updstmt ) {
zero or more statements
}
• do while loop
block of statements is executed first and then condition is checked
Syntax : do {
zero or more statements
}while ( test condition) Loop.html (Loop.html.HTM)
19
Amity School of Engineering & Technology
function myfunction(argument1,argument2,etc) {
some statements;
}
There are two purposes for functions.
1. It is a reusable code which is used to perform the same operation without copying the code again and
again(organizational tool).
2. It is link to event and the web page. eg radio button, Mouse clicks, button presses, etc.
Amity School of Engineering & Technology
Top-Level functions
• eval
– Evaluates a string without reference to a particular object.
– eval( string)
• parseInt and parseFloat
– Converts the string value to numeric value.
– parseInt( string) , Syntax parseFloat( string)
• isNaN
– Determines whether the parameter is NaN (no ta number).
– isNaN( testValue)
• isFinite
– Determines whether it is a finite number
– isFinite( number)
funTop.html (funTop.html.HTM)
23
Amity School of Engineering & Technology
In-built properties
topproperty.html (topproperty.html.HTM)
25
Amity School of Engineering & Technology
Summary
26