Javascript & DOM.

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.
Java Script Session1 INTRODUCTION.
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.
CST JavaScript Validating Form Data with JavaScript.
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) JAVA Script James Wang.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
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.
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.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Javascript & DOM. Javascript – main properties is a lightweight scripting language (language used to control the browser) that is run on the client-side.
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)
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Using Client-Side Scripts to Enhance Web Applications 1.
 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-
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.
Scripting and Interactivity 이병희
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
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.
This is our seminar JavaScript And DOM This is our seminar JavaScript And DOM.
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:
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Introduction to.
CGS 3066: Web Programming and Design Spring 2017
DHTML.
Module 1 Introduction to JavaScript
Chapter 5 Validating Form Data with JavaScript
Java Script Introduction. Java Script Introduction.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
“Under the hood”: Angry Birds Maze
Tutorial 10 Programming with JavaScript
Chapter 4 Client-Side Programming: the JavaScript Language
JavaScript is a programming language designed for Web pages.
JavaScript Fundamentals
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
14 A Brief Look at JavaScript and jQuery.
JavaScript Introduction
JavaScript an introduction.
Web Systems Development (CSC-215)
DHTML Javascript Internet Technology.
WEB PROGRAMMING JavaScript.
DHTML Javascript Internet Technology.
PHP.
Tutorial 10 Programming with JavaScript
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
JavaScript Basics What is JavaScript?
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Web Programming and Design
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 ECMAScript 2016 (2016)

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 <head> 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” An object is an associative array augmented with a prototype 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) <TAG eventHandler=“Javascript code”> 2) <script type=“text/javascript”> function evHandle(x) { … } </script> <TAG eventHandler=“evHandle(this)”> 3) <script type="text/javascript"> 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 <SCRIPT> tag: <script type=”text/javascript”> … JavaScript statements... </script> 2) Js code written in a separate javascript file: <script src="common.js"></script> 3)  using a Js expression as the value of an html attribute: <hr width="&{barWidth};%" align=“left"> <h4>&{myTitle};</h4> JavaScript entities start with “&” and end with “;” and are enclosed in “{}” 4)  as an event handler: <input type="button" value="Press Me" onClick="func()">

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 Description 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 Property Description body Gives direct access to the <body> element cookie Sets or returns all cookies associated with the current document domain Returns the domain name for the current document lastModified Returns the date and time a document was last modified referrer Returns the URL of the document that loaded the current document title Returns the title of the current document URL Returns the URL of the current document

Document object methods Description 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 http://www.cs.ubbcluj.ro/~forest/wp http://www.w3schools.com/js/default.asp http://www.w3schools.com/jsref/dom_obj_document.asp http://devedge-temp.mozilla.org/central/javascript/index_en.html https://developer.mozilla.org/en/JavaScript