Javascript & DOM. Javascript – main properties is a lightweight scripting language (language used to control the browser) that is run on the client-side.

Slides:



Advertisements
Similar presentations
Chapter 7 JavaScript: Introduction to Scripting. Outline Simple Programs Objects and Variables Obtaining User Input with prompt Dialogs – –Dynamic Welcome.
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Intro to JavaScript. JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted.
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
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
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Tutorial 10 Programming with JavaScript
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
WTP Unit 4 JavaScript and DHTML. Javascript: – Client side scripting, – What is Javascript, – How to develop Javascript, – Simple Javascript, – Variables,
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
CST JavaScript Validating Form Data with JavaScript.
JavaScript and The Document Object Model MMIS 656 Web Design Technologies Acknowledgements: 1.Notes from David Shrader, NSU GSCIS 2.Some material adapted.
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
Introduction to JavaScript. JavaScript Facts A scripting language - not compiled but interpreted line by line at run-time. Platform independent. It is.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) JAVA Script James Wang.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
The Document Object Model. Goals Understand how a JavaScript can communicate with the web page in which it “lives.” Understand how to use dot notation.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Database-Driven Web Sites, Second Edition1 Chapter 3 INTRODUCTION TO CLIENT-SIDE SCRIPTS.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaScript Fundamentals.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
1 CLIENT-SIDE SCRIPTS. Objectives 2 Learn how to reference objects in HTML documents using the HTML DOM and dot syntax Learn how to create client-side.
CP476 Internet Computing JavaScript Client-Side Programming 1 1. What is JavaScript –Another script language for both client-side and sever-side programming.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
SE-2840 Dr. Mark L. Hornick 1 Modifying webpage behavior using client-side scripting Javascript.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
JavaScript Introduction 1. What is Java Script ? JavaScript is a client-side scripting language. A scripting language is a lightweight programming language.
JavaScript - A Web Script Language Fred Durao
JS Basics 1 Lecture JavaScript - Basics. JS Basics 2 What is JavaScript JavaScript is a “simple”, interpreted, programming language with elementary object-
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
JavaScript Introduction.  JavaScript is a scripting language  A scripting language is a lightweight programming language  A JavaScript can be inserted.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
1 JavaScript in Context. Server-Side Programming.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Java Script About Java Script Document Object Model Incorporating JavaScript Adding JavaScript to HTML Embedding a Javascript External Scripts Javascript.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
“Under the hood”: Angry Birds Maze
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
14 A Brief Look at JavaScript and jQuery.
JavaScript an introduction.
Web Systems Development (CSC-215)
WEB PROGRAMMING JavaScript.
JavaScript CS 4640 Programming Languages for Web Applications
Javascript & DOM.
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

Javascript & DOM

Javascript – main properties is a lightweight scripting language (language used to control the browser) that is run on the client-side (browser) developed by Netscape and Sun Microsystems and introduced first in the Netscape Navigator 2.0 is intended to add interactivity and dynamic functionality to HTML pages is interpreted, not compiled, inserted directly in HTML pages is an object-oriented language that inherits many features from Java, but it is not Java is understood by most browsers is an event-based language, weakly typed current version is 1.9 (2009)

What can Javascript do ? it can detect the type of browser it can react to various events of the browser it can alter the structure of the html document (modify, delete, add tags on the run-time) it can validate data before being sent to the server it can not modify local (client) files

Base elements of Javascript Js inherits from Java simple data types, operators, instruction syntax Js has predefined objects: DOM-related and general objects: Array, Boolean, Date, Error, EvalError, Function, Math, Number, Object, Range Error, ReferenceError, RegExp, String, SyntaxError, TypeError, URIError Js has some global functions (not related to objects): decodeURI, decodeURIComponent, encodeURI, encodeURIComponent, eval, isFinite, isNaN, parseFloat, parseInt comments: // or /*…*/ Js is weakly-typed: a variable can be bound to a specific type, then to a different type, during the course of a program execution

Types and literals (constant values) numbers: integer (in base 2, 8, 10, 16) and real boolean: true and false null undefined: a variable that does not have an assigned value; NaN: not a number String: ‘text’, “something” etc.; methods of the string class can be applied on any string literal (e.g. “sir”.length) vectors: [‘a’,,, ‘bbb’, ‘ccc’] will have 5 elements objects: lists of zero or more pairs “property : value” ex.: dog = {name: dog, type: animal, characteristics: getProps(“dog”), age: 4}

Variables loosly-typed language: a variable can be bound to different types during its lifetime; the value of a variable is automatically converted to other types when necessary the type of a variable needs not be declared a variable is declared with “var” or just by assigning a value to it: var name; root=“some text”; i = root+1; // i=“some text1” a variable without a value assigned to it will be evaluated to “undefined” or NaN (depending on the context) if it was declared with “var” or will give a run- time error if it was not declared with “var”

Operators 3 types of expressions in Js: arithmetic (eval. to a number), string and boolean (eval. to true or false) assign operators: =, +=, -=, *=, /=, %=, >=, >>>=, &=, ^=, |= comparison operators: ==, !=, >, >=, <, <= arithmetic operators: %, /, ++, --, +, - bitwise operators: &, |, ^, ~, >>, >> logic operators: &&, ||, ! string operator: + (concatenation) special operators

Special operators identity operators: === (eguality and of the same type), !== (not equal and/or of different types) ternary operator: condition ? true-expr : false-expr comma: expr1, expr2, …, exprN new: creates a new object this: refers to the calling object in a method typeof: typeof(“test”) => string delete: deletes an object or a property from an object or an element from a vector in: propNameorNumber in objectName instanceof: objectName instanceof objectType void: evaluates an expression without returning a value

Instructions (borrowed from Java) conditional: if, switch loop: for, do while, while, label, break, continue for (variable in object) { … statements …} : cycles through thee properties of an object with (object) { … statements … } : sets the default object for a set of statements exception handling instructions: try { … statements … } catch (exception) { … } throw expression;

Functions usually they are declared in the tag and called all over the html file the syntax of declaring a function is: function name_fct(parameters, arguments) { … statements … } where parameters represent specific parameters sent to the function, arguments contain a variable number of arguments; the variable arguments can be accessed inside the function through arguments[i],where i goes from 0 to arguments.length all arguments are passed to the function through value; only object properties changes are visible outside the function

Classes and objects Js is a prototype-based language, it does not distinct between a class and a class instance (object); it only has objects; current object referred with “this” creating objects can be done in 2 ways: using an object initializer: objectName = {property1:value1, property2:value2,..., propertyN:valueN} using a constructor function: function print() {…} function thing(x, y, z) { prop1=x; prop2=y; prop3=z; method1=print;} ob = new thing(a, b, c); objects are deleted using “delete objectName” properties are accessed using obj.property or obj[index_property] new properties can be added to object on run-time: obj.newProp=val

Predefined objects Array – working with arrays (sort, push etc.) Boolean – true or false Function – specifies a string of code to be precompiled as a function Date – date functions Math – math functions Number – numerical constants and representations RegExp – regular expressions String – string operations

Events Javascript is an event-based language Event: mouse click, key pressed, element loosing focus etc. when an event is triggered by the browser a predefined or user-defined (in Javascript) event handler takes control event handlers are associated to a tag: 1) 2) function evHandle(x) { … } 3) obj.eventHandler=“Javascript code”;

Events (2)

Javascript and HTML Js scripts can be used in HTML documents in 4 ways: 1) as instructions or functions written inside a tag: … JavaScript statements... 2) Js code written in a separate javascript file: 3) using a Js expression as the value of an html attribute: &{myTitle}; JavaScript entities start with “&” and end with “;” and are enclosed in “{}” 4) as an event handler:

Pop-up boxes alert(“…text…”) : displays text and the Ok button confirm(“… text…”) : displays text and returns true if the Ok button is clicked and false if the Cancel button is clicked prompt(“text”, “default value”) : the user can enter an input value and then click Ok (return the value) or Cancel (return null)

DOM (Document Object Model) is a standardized (by W3C) hierarchical model of an HTML or XML document DOM can be used for navigating in the document structure, modify the document structure (add, delete, modify child elements etc.) and also modifying attributes of an element each tag is a DOM object it has an API which can be used in Javascript Javascript + DOM is sometimes called DHTML (Dynamic HTML)

DOM Browser Objects Window object Navigator object Screen object History object Location object

DOM document objects Document object Anchor object Area object Base object Body object Button object Event object Form object Frame object Frameset object IFrame object Image object Input Button object Input Checkbox object Input File object Input Hidden object Input Password object Input Radio object Input Reset object Input Submit object Input Text object Link object Meta object Object object Option object Select object Style object Table object TableCell object TableRow object Textarea object

Document object collections CollectionDescription forms[]Returns a reference to all Form objects in the document images[]Returns a reference to all Image objects in the document links[]Returns a reference to all Area and Link objects in the document anchors[]Returns a reference to all Anchor objects in the document

Document object properties PropertyDescription bodyGives direct access to the element cookieSets or returns all cookies associated with the current document domainReturns the domain name for the current document lastModifiedReturns the date and time a document was last modified referrerReturns the URL of the document that loaded the current document titleReturns the title of the current document URLReturns the URL of the current document

Document object methods MethodDescription close()Closes an output stream opened with the document.open() method, and displays the collected data getElementById()Returns a reference to the first object with the specified id getElementsByName()Returns a collection of objects with the specified name getElementsByTagName()Returns a collection of objects with the specified tagname open()Opens a stream to collect the output from any document.write() or document.writeln() methods write()Writes HTML expressions or JavaScript code to a document writeln()Identical to the write() method, with the addition of writing a new line character after each expression

Additional Documentation