Java Script.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
The Web Warrior Guide to Web Design Technologies
JavaScript (Part 2) CS 183 4/13/2010. JavaScript (Part 2) Will cover: ◦ For.. In ◦ Events ◦ Try.. Catch, Throw ◦ Objects.
Lecture 1 Term 2 9/1/12 1. Objects You normally use the “.” Operator to access the value of an object’s properties. The value on the left of the “.” should.
Computer Science 103 Chapter 4 Advanced JavaScript.
JavaScript Objects Objects – JavaScript is an object-based language Has built-in objects we will use – You can create your own objects We concentrating.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
1 CS101 Introduction to Computing Lecture 35 Mathematical Methods (Web Development Lecture 12)
JS: DOM Form Form Object Form Object –The Form object represents an HTML form. –For each instance of a tag in an HTML document, a Form object is created.
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Scripting Languages.
Lectured By: Vivek Dimri Asst. Professor, SET, Sharda University
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS.
Bridges To Computing General Information: This document was created for use in the "Bridges to Computing" project of Brooklyn College. You are invited.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Internet Mark-up Languages CO32036 Part Lecture: Elementary JavaScript.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
JavaScript For...In Statement The for...in statement loops through the elements of an array or through the properties of an object. Syntax for (variable.
Objects.  Java Script is an OOP Language  So it allows you to make your own objects and make your own variable types  I will not be going over how.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Pemrograman Teknologi Internet W06: Functions and Events.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
Keeping it Neat: Functions and JavaScript Source Files Chapter 7.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
1 JavaScript 2 JavaScript. 2 Rollovers Most web page developers first use JavaScript for rollovers A rollover is a change in the appearance of an element.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
JavaScript, Fourth Edition
CSCE 102 – Chapter 11 (Performing Calculations) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
Making dynamic pages with javascript Lecture 1. Java script java versus javascript Javascript is a scripting language that will allow you to add real.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
JavaScript Event Handlers. Introduction An event handler executes a segment of a code based on certain events occurring within the application, such as.
Tutorial 11 1 JavaScript Operators and Expressions.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
COM621: Advanced Interactive Web Development Lecture 5 – JavaScript.
REEM ALMOTIRI Information Technology Department Majmaah University.
Java Script Introduction. Java Script Introduction.
Chapter 6 JavaScript: Introduction to Scripting
BIL 104E Introduction to Scientific and Engineering Computing
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Programming the Web using XHTML and JavaScript
JavaScript: Functions
SEEM4570 Tutorial 05: JavaScript as OOP
JavaScript Functions.
Week#3 Day#1 Java Script Course
JavaScript Syntax and Semantics
Fundamental of Java Programming Basics of Java Programming
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
14 A Brief Look at JavaScript and jQuery.
BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES
Event Driven Programming & User Defined Functions
PHP.
JavaScript Defined General Syntax Body vs. Head Variables Math & Logic
Functions, Regular expressions and Events
JavaScript: Introduction to Scripting
محمد احمدی نیا JavaScript محمد احمدی نیا
JavaScript and Ajax (JavaScript Events)
Web Programming and Design
Presentation transcript:

Java Script

JavaScript Date Object The Date object is used to work with dates and times.  Date objects are created with the Date() constructor. There are four ways of instantiating a date: new Date() // current date and time new Date(milliseconds) //milliseconds since 1970/01/01 new Date(dateString) new Date(year, month, day, hours, minutes, seconds, milliseconds)

JavaScript Date Object Once a Date object is created, a number of methods allow you to operate on it. Most methods allow you to get and set the year, month, day, hour, minute, second, and milliseconds of the object, using either local time or UTC (universal, or GMT) time. All dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds.

Set Dates We can easily manipulate the date by using the methods available for the Date object. In the example below we set a Date object to a specific date (14th January 2010): var myDate=new Date(); myDate.setFullYear(2010,1,14); And in the following example we set a Date object to be 5 days into the future: var myDate=new Date(); myDate.setDate(myDate.getDate()+5);

Date Object Properties Property Description constructor Returns the function that created the Date object's prototype prototype Allows you to add properties and methods to an object

Date Object Methods Method Description getDate() Returns the day of the month (from 1-31) getDay() Returns the day of the week (from 0-6) getFullYear() Returns the year (four digits) getHours() Returns the hour (from 0-23) getMilliseconds() Returns the milliseconds (from 0-999) getMinutes() Returns the minutes (from 0-59) getMonth() Returns the month (from 0-11) getSeconds() Returns the seconds (from 0-59) getTime() Returns the number of milliseconds since

Date Object Methods setDate() Sets the day of the month (from 1-31) setFullYear() Sets the year (four digits) setHours() Sets the hour (from 0-23) setMilliseconds() Sets the milliseconds (from 0-999) setMinutes() Set the minutes (from 0-59) setMonth() Sets the month (from 0-11) setSeconds() Sets the seconds (from 0-59) setTime() Sets a date and time by adding or subtracting a specified number of milliseconds to/from

JavaScript Math Object The Math object allows you to perform mathematical tasks The Math object includes several mathematical constants and methods. var pi_value=Math.PI; var sqrt_value=Math.sqrt(16); Math is not a constructor. All properties and methods of Math can be called by using Math as an object without creating it.

Mathematical Constants JavaScript provides eight mathematical constants that can be accessed from the Math object. These are: E, PI, square root of 2, square root of 1/2, natural log of 2, natural log of 10, base-2 log of E, and base-10 log of E.

Mathematical Constants Math.E Math.PI Math.SQRT2 Math.SQRT1_2 Math.LN2 Math.LN10 Math.LOG2E Math.LOG10E

Mathematical Methods In addition to the mathematical constants that can be accessed from the Math object there are also several methods available. The following example uses the round() method of the Math object to round a number to the nearest integer: document.write(Math.round(4.7));

Math Object Methods Method Description abs(x) Returns the absolute value of x acos(x) Returns the arccosine of x, in radians asin(x) Returns the arcsine of x, in radians atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians atan2(y,x) Returns the arctangent of the quotient of its arguments ceil(x) Returns x, rounded upwards to the nearest integer cos(x) Returns the cosine of x (x is in radians) exp(x) Returns the value of Ex floor(x) Returns x, rounded downwards to the nearest integer

Math Object Methods log(x) Returns the natural logarithm (base E) of x max(x,y,z,...,n) Returns the number with the highest value min(x,y,z,...,n) Returns the number with the lowest value pow(x,y) Returns the value of x to the power of y random() Returns a random number between 0 and 1 round(x) Rounds x to the nearest integer sin(x) Returns the sine of x (x is in radians) sqrt(x) Returns the square root of x tan(x) Returns the tangent of an angle

FUNCTIONS IN JAVASCRIPT JavaScript provides several built-in functions that can be used to perform explicit type conversions. Example: The following results in the value 105 being assigned to the variable grand_Total. var grand_Total= eval(“l0 * 10 + 5”);

USER DEFINED FUNCTIONS Functions offer the ability to group together JavaScript program code that performs a specific task into a single unit that can be used repeatedly whenever required in a JavaScript program. A user defined function first needs to be declared and coded. Once this is done the function can be invoked by calling it using the name given to the function when it was declared.

Declaring Functions Functions are declared and created using the function keyword. A function can comprise of the following: A name for the function A list of parameters (arguments) A block of JavaScript code Syntax: function function_name(parameterl, parameter2, . . .) { /1 Block of JavaScript code }

A function_name is case sensitive, can include underscores and has to start with a letter. The list of parameters passed to the function appears in parentheses and commas separate members of the list. Note: Defining a function does not execute the JavaScript code that makes up the function.

Place of Declaration Functions can be declared anywhere within an HTML file. Preferably, functions are created within the <HEAD>...</HEAD> tags of the HTML file. This ensures that all functions will be parsed before they are invoked or called. If the function is called before it is declared and parsed, it will lead to an error condition, as the function has not been evaluated and the ‘Browser’ does not know that it exists.

Passing Parameters Values can be passed to function parameters when a ‘parameterized’ function is called. Values are passed to the function by listing them in the parentheses following the function name. Multiple values can be passed, separated by commas provided that the function has been coded to accept multiple parameters.

Function Declaration: function printName(user) { document.write(“<HR>Your Name is <B><l>”); document.write(user); document.write(”</B></l><HR>”); } where, printName is a function, which has a parameter called user. The parameter user can be passed a value at the time of invoking the function. Within the function, reference to user will then refer to the value passed to the function.

Function Call: A static value passed: printName(”Bob”); (will cause the string “Bob” to be assigned to the parameter user.) A Variable Passed: var user = “John”; printName(user); (will cause the value “John” to be assigned to the parameter user.)

<HTML> <HEAD> <TITLE>Creating and Using User Defined Functions</TITLE> <SCRIPT Language=”JavaScript”> var name =””; function hello() { name = prompt(’Enter Your Name:’, ‘Name’); alert(’Greetings ‘+ name +‘, Welcome to my page!’); } function goodbye() { alert(’Goodbye ‘ + name +‘, Sorry to see you go!’); } } <JSCRIPT> </HEAD> <BODY onLoad=hello(); onUnload=goodbye();> <1MG SRC=”images/Pinkwhit.gif’> </BODY> </HTML>

Variable Scope The parameters of a function are local to the function. They come into existence when the function is called and cease to exist when the function ends. For instance, in the example printName( ), user exists only within the function printName( ) - it cannot be referred to or manipulated outside the function. Also, any variable declared using var variable-name within the function would have a scope limited to the function.

Variable Scope If a variable is declared outside the body of the function, it is available to all statements within the JavaScript. If a local variable is declared within a function has the same name as an existing global variable, then within the function code, that variable .name will refer to the local variable and not the global variable. It is as though the global variable does not exist with respect to the JavaScript code within the function.

Return Values As with some JavaScript built-in functions, user defined functions can return values. Such values can be returned using the return statement. The return statement can be used to return any valid expression that evaluates to a single value. Example: function cube(number) { var cube = number * number * number; return cube; }

Recursive Functions Recursion refers to a situation, wherein functions call themselves. In other words, there is a call to specific function from within the same function. Such functions are known as Recursive Functions

example function factorial(number) { if(number> 1) { return number * factorial(number- 1); } else { return number; }

Recursive Functions Note: Recursive functions are powerful, but can lead to infinite recursions. Infinite recursions occur when the function is designed in such a way as to call itself without being able to stop.

PLACING TEXT IN A BROWSER Using JavaScript a string can be written to the browser from within an HTML file. The document object in JavaScript has a method for placing text in a browser. This method is called write ( ). Methods are called by combining the object name with the method name: Object-name. Method-Name

The write() method accepts a string value passed to it within its parentheses. The string value can then be written to the browser. The write( ) method accepts this string and places it in the current browser window. For example: document.write(”Test”); The string ‘Test” will be placed in the browser.

Events A mouse click A web page or an image loading Mousing over a hot spot on the web page Selecting an input field in an HTML form Submitting an HTML form A keystroke

onLoad and onUnload The onLoad and onUnload events are triggered when the user enters or leaves the page. Both the onLoad and onUnload events are also often used to deal with cookies that should be set when a user enters or leaves a page.

onFocus, and onChange The onFocus, onBlur and onChange events are often used in combination with validation of form fields.

onSubmit The onSubmit event is used to validate ALL form fields before submitting it. Below is an example of how to use the onSubmit event.

onMouseOver and onMouseOut onMouseOver and onMouseOut are often used to create "animated" buttons. Below is an example of an onMouseOver event.