Upload
sara-hicks
View
223
Download
1
Tags:
Embed Size (px)
Citation preview
Mahesh Chandran Pillai
JavaScript is an implementation of the ECMAScript language standard and is typically used to enable programmatic access to computational objects within a host environment.
It can be characterized as a prototype-based object-oriented scripting language that is dynamic, weakly typed and has first-class functions
Mahesh Chandran Pillai
JavaScript is primarily used in the form of client-side JavaScript, implemented as part of a web browser in order to provide enhanced user interfaces and dynamic websites.
Mahesh Chandran Pillai
Put dynamic text into an HTML page React to events Read and write HTML elements Validate data Detect the visitor’s browser Create cookies
Mahesh Chandran Pillai
Access other resources such as Files, Databases and Programs
Talk to Web Servers Give similar output on different
browsers Be encrypted. Be used with copyrighted data or
algorithms
Mahesh Chandran Pillai
A script that is executed by the browser on a users computer
Instead of the entire page, part of the page is sent to the browser when user requests for the page.
Mostly run in response to an event For example: Click events, validations. Direct interaction with client
Mahesh Chandran Pillai
Requirements› A Text Editor like Notepad, Notepad++, etc….› Knowledge of basic HTML and form Tags
Ways for Including JavaScript in your HTML page› Direct Insertion
Using <SCRIPT> …….</SCRIPT> inside <HEAD> or <BODY> tags
› Embedded Insertion Using “Javascript” as prefix in other HTML tags. <input type=“button”
onclick=‘JavaScript:alert(“Hi”);’ />› External References
By specifying the “src” attribute in <SCRIPT> tag. <SCRIPT type=“text/javascript” src=“filename.js”/>
Mahesh Chandran Pillai
<html><head>
<title>JavaScript Skeleton</title><script type = "text/javascript">
// JavaScript can go here!// But no HTML!
</script></head><body>
<script type = "text/javascript">// JavaScript can go here too!// But no HTML!</script>
</body></html>
Mahesh Chandran Pillai
// - single line commenting /*…..*/ - multiple lines commenting <!- - // - - > - Commenting Javascript
from old browsers<script type = "text/javascript"><!--hide me from older browsers// say Hello, world!alert("Hello, world!");// show me -->
Mahesh Chandran Pillai
JavaScript is case-sensitive ƒ Ending statements with a semicolon is
optional. ƒ Open and Close braces for scoping
the embedded Java Script code is mandatory.
ƒ You can apply more than one script in a same HTML file.
Mahesh Chandran Pillai
Values, Variables, and Literals Expressions and Operators Regular Expressions Statements Functions Objects
Mahesh Chandran Pillai
JavaScript does not have explicit data types There is no way of specifying that a particular �
variable represents an integer, a string or a real.
All JavaScript variables are declared applying the keyword var.› For example: var x, y=7› Var x=“hello guys”› Y=true
JavaScript recognizes strings, numbers, Boolean values and null
Mahesh Chandran Pillai
Rules for identifiers Should start with a letter or an underscore (
_ ). Case sensitive. Numbers are possible in a
name.
Scope of variables Local variables: declared inside a function
using var keyword. Global Variables: declared outside the
functions.Mahesh Chandran Pillai
Operators:› » >=, ==, +=, ||, <, >, and so on.
Control statements:› If, if….else, for, and so on.
Keywords:› Var, true, false, new, return
Mahesh Chandran Pillai
if.. .. else› Syntax:if (condition){code to be executed if
condition is true}else{code to be executed if condition is not true}
if.. …else if…. else.if (condition1) { code to be executed } else if (condition2) { code to be executed } else { code to be executed }
Mahesh Chandran Pillai
Switch …. Case› Syntax:
switch(n) {case 1: execute code block 1 break ;case 2: execute code block 2 break ;default: code to be executed if n is different from case 1 and 2 }
Mahesh Chandran Pillai
For Loop› The for loop is applied when you know in
advance, how many times the script should run.
› For loop Syntax:var initval;for(initval=startvalue; initval<=endalue;
initval=initval+incrval) { code to be executed}
Mahesh Chandran Pillai
While › Syntax:
while (initval<=endvalue) { code to be executed}
Do..While › Syntax:
do { code to be executed } while (var<=endvalue)
Mahesh Chandran Pillai
For … in Loop› The for..in statement is applied to loop
(iterate) through the elements of an array or through the properties of an object.
› Syntax:for (variable in object) { code to be executed}
Mahesh Chandran Pillai
Functions can be of two types. Built in Functions
› Functions which are provided by Javascript. User Defined Functions
› A function is identified by the keyword function, followed by a programmer-supplied name for the function, and followed by a set of parentheses that optionally enclose data values needed by the function to carry out its processing.
› Syntax:function function_name(parameters) {
function-body; }
Mahesh Chandran Pillai
Calling Functions› You can call a function by using it name
followed by parenthesis with parameters if defined.Function_name(parameters);
› You can call functions with event handlers <Input type=“button” id=“Btn1" onclick=“fun1()“/>
› You can avoid functions by writing inline scripts It is useful only for avoiding small functions.
Mahesh Chandran Pillai
Alert( message)› Which puts a string into a little
announcement box (also called an alert box).› Syntax: alert(“I will show this message!");
Prompt(message, txtmsg)› which asks your visitor for some information
and then sets a variable equal to whatever your visitor types.
› Syntax: var name = prompt("What's your name?", "put your name here");
Confirm()› Which confirms the users action and returns a
Boolean value.› Syntax: confirm(“Do you want to continue”);
Mahesh Chandran Pillai
Mahesh Chandran Pillai
Examples: alert("Hello, world!");
prompt("Pls enter u r name", "enter here ");
confirm("Do you want to continue");
document.write (str)› This function will help you to write any
string message to the web page.
Mahesh Chandran Pillai
Date› The Date Object type provides a common set of
methods for working with dates and times. String
› The String Object type provides a set of methods for
› manipulating strings. Math
› The Math Object type provides a common set of methods for working with mathematical tasks.
Mahesh Chandran Pillai
Name Description
getDate() The day of the month as an integer from 1 to 31
getDay() The day of the week as an integer where 0 is Sunday and 1 is Monday
getHours() The hour as an integer between 0 and 23
getMinutes() The minutes as an integer between 0 and 59
getMonth() The month as an integer between 0 and 11 where 0 is January and 11 is December
getSeconds() The seconds as an integer between 0 and 59
getTime() The current time in milliseconds where 0 is January 1, 1970, 00:00:00
getYear() The year, but this format differs from browser to browser
Mahesh Chandran Pillai
substring(startIndex) substring(startIndex,endIndex) charAt(index) toLowerCase() toUpperCase() toString() valueOf() Indexof()
Mahesh Chandran Pillai
Name Description
exp() Value of e raised to the power passed as the argument
Abs() Absolute value of the
sqrt() Square root of the argument
round() Argument rounded up if its decimal value is greater than or equal to 0.5 and rounded down otherwise
pow() First argument raised to the power passed as the second argument
random() Random number between 0 and 1
min() Lower of the two numbers passed as arguments
Max() Higher of the two numbers passed as arguments
Log() Natural log of the argument
floor() Integer lower than or equal to the number passed as the argument
ceil() Integer greater than or equal to the number passed
Mahesh Chandran Pillai
Mahesh Chandran Pillai
Name Description
escape(string) Encodes a string from ASCII into an ISO Latin-1.
Unescape() encodes a string from ISO Latin-1 to ASCII
Eval() Converts a string to integer or float value. It can also evaluate expressions included with a string.
isNaN(value) If the value passed is a not a number, the Boolean value of true is returned, if it is a number, it returns false.
typeof operator This is an operator but its usefulness is similar to a function. This operator returns the type of the object it operates on.
parseInt() Converts a string to an integer returning the first integer encountered which is contained in the string.
parseFloat() Returns floating point numbers the same as the parseInt function, but looks for floating point qualified strings and returns their value as a float.
toString() Converts an object to a string
A collection elements in contiguous memory location.
Creating Arrays› Syntax
Var arr_name=new Array(elements);› Example
var colors = new Array("red", "orange", "yellow", "green", "blue", "indigo", "violet");
Assigning Valuesvar the_answers = new Array();the_answers[0] = "yes";the_answers[1] = "no“;
Mahesh Chandran Pillai
An associative array uses strings instead of numbers to store values.
Examples:var phone_book = new Array();phone_book[“devu"] = "(415) 555-5555";phone_book["info"] = "(415) 555-1212";
Length property of array will help you to find the number of elements.
Mahesh Chandran Pillai
onLoad onMouseOut and onMouseOver onReset onSelect onSubmit onUnload onAbort onBlur onChange onClick onError onFocus OnKeyup OnKeyDown OnKeyPress
Mahesh Chandran Pillai
Event handlers are functions are invoked when an event is raised or fired by the object.
Examples:› <a href = "#" onMouseOver = "alert(‘Mouse
overboard!');">board</a>› <a href = “hello.html" onClick = "alert('I\'m
glad you have JavaScript turned on!'); return false;">Click me</a>
› <a href = "#“ onClick = “var the_color = prompt('red or blue?',''); window.document.bgColor = the_color; return false;">change background</a>
Mahesh Chandran Pillai
Mahesh Chandran Pillai
The document object lists all the images, links, forms, and other stuff on a web page.
Mahesh Chandran Pillai
Mahesh Chandran Pillai