Making web pages interactive with JavaScript

Slides:



Advertisements
Similar presentations
© 2010, Robert K. Moniot Chapter 5 CSS : Cascading Style Sheets 1.
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
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
Javascript Document Object Model. How to use JavaScript  JavaScript can be embedded into your html pages in a couple of ways  in elements in both and.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
CM143 - Web Week 2 Basic HTML. Links and Image Tags.
The Browser Object Model (BOM) The API of JavaScript’s browser host environment SE-2840 Dr. Mark L. Hornick 1.
CSS (Cascading Style Sheets): How the web is styled Create Rules that specify how the content of an HTML Element should appear. CSS controls how your web.
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
4.1 JavaScript Introduction
JS: Document Object Model (DOM)
CSS Positioning Creating Special Effects with CSS CS202 Working with Cascading Style Sheets (Chapter 4) CS202 1.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
Unobtrusive JavaScript
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
History, Navigator, Screen and Form Objects Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan 1.
1 JavaScript: Objects and Object Models October 25, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel,
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
XP Dreamweaver 8.0 Tutorial 3 1 Adding Text and Formatting Text with CSS Styles.
SE-2840 Dr. Mark L. Hornick 1 Modifying webpage behavior using client-side scripting Javascript.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
JavaScript – The DOM JavaScript is object based The browser is object based – We can access the browser's objects in the same way we did JavaScript's Two.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
Tutorial 5 Windows and Frames Section B - Working with Frames and Other Objects Go to Other Objects.
XP Tutorial 6 New Perspectives on JavaScript, Comprehensive1 Working with Windows and Frames Enhancing a Web Site with Interactive Windows.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Introduction to JavaScript CS101 Introduction to Computing.
Web Development 101 Presented by John Valance
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
JavaScript DOM.
Tutorial 3 Adding and Formatting Text with CSS Styles.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Topic 5 Windows and Frames. 2 Goals and Objectives Goals Goals Understand JavaScript window object, how popup windows work, find the browser that a client.
Host Objects: Browsers and the DOM
JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Making web pages interactive with JavaScript.
Chapter 4 Frames and Cascading Style Sheets. Frames Frames divide a browser window into two or more separate pieces or panes, with each pane containing.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
COM621: Advanced Interactive Web Development Lecture 6 – JavaScript (cont.)
1 The Document Object Model. 2 Objectives You will be able to: Describe the structure of the W3C Document Object Model, or DOM. Use JavaScript to access.
Introduction to.
DHTML.
Module 1 Introduction to JavaScript
JavaScript, Sixth Edition
Programming Web Pages with JavaScript
Applied Component I Unit II Introduction of java-script
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Webpage layout using CSS
Donna J. Kain, Clarkson University
Web Development & Design Foundations with HTML5 7th Edition
Document Object Model (DOM): Objects and Collections
The Document Object Model
Web Systems Development (CSC-215)
Document Object Model That’s DOM to you
WEB PROGRAMMING JavaScript.
Tutorial 6 Creating Dynamic Pages
Introduction to Programming the WWW I
Working with Dynamic Content and Styles
Chapter 16 The World Wide Web.
Tutorial 4 Creating Special Effects with CSS
PHP an introduction.
Lesson 2: HTML5 Coding.
Web Client Side Technologies Raneem Qaddoura
Creating dynamic/interactive web pages
Web Programming and Design
Presentation transcript:

Making web pages interactive with JavaScript Dynamic HTML Making web pages interactive with JavaScript SE-2840 Dr. Mark L. Hornick

JavaScript “core API” defines only a few native objects – the remainder come from the hosting environment (i.e. the browser) String – similar to the Java String class Array – generic container/collection class Math - like the Java Math class Number, Boolean – wrapper classes similar to Java wrapper classes (Integer, Double etc) var x = 123; // x is treated as a Number var y = “123”; // y is treated as a String var z = new Number(“123”); // z is a Number Date – represents dates and times CS-422 Dr. Mark L. Hornick

Part 1: The Browser Object Model (BOM) SE-2840 Dr. Mark L. Hornick

The BOM is a set of JavaScript-accessible objects that comprise various elements of a Browser These are instances of classes defined by the Browser i.e. they are not native to Javascript like String, Date, etc That is, not part of the JavaScript core API objects The BOM today has been standardized across current versions of browsers Until very recently different browsers implemented BOM objects differently SE-2840 Dr. Mark L. Hornick

window is the top-level object in the BOM hierarchy prompt(), and alert() are methods of the browser’s window object You can call alert either with: window.alert(“Hello”); Or just: alert(“Hello”); window is a global object, which means you don’t need to use its name to access its properties and methods Also: every global variable and function you define becomes part of the window object due to JavaScript object extension. SE-2840 Dr. Mark L. Hornick

Some window methods and attributes alert() – posts a message dialog confirm("question") - returns true or false prompt("question", "default") - returns a String open() - open a new browser window close() – close a window Properties (attributes) defaultStatus – text in status bar name – name of the window opener – window that created this window outerwidth, outerheight – window extents Note: The window object contains many other methods and properties for various purposes – view them from within the browser debugger SE-2840 Dr. Mark L. Hornick

Other BOM objects are children of the window object Note: This list is not complete Represents information about the Browser and the underlying OS Information about the display capabilities of the client PC running the Browser Information on recently visited sites Information on current URL Represents the current web page – the DOM SE-2840 Dr. Mark L. Hornick

Some navigator methods and attributes javaEnabled() – returns true or false Geolocation – new HTML5 Properties (attributes) appCodeName – code name of the browser appName - name of the browser appVersion- platform and version of browser cookieEnabled – true if cookies are enabled userAgent – user-agent header sent to server SE-2840 Dr. Mark L. Hornick

Some screen methods and attributes none Properties (attributes) availHeight, availWidth, height, width – metrics of display screen size bufferDepth, colorDepth, pixelDepth – metrics of the color palette deviceXDPI, deviceYDPI, logicalXDPI, logicalYDPI - number of dots per inch of the display screen updateInterval – refresh interval SE-2840 Dr. Mark L. Hornick

Some history methods and attributes back() – loads the previous URL in the history list forward() – loads the next URL in the history list go() – loads specific page in the history list Properties (attributes) length – number of elements in the history list SE-2840 Dr. Mark L. Hornick

Some location methods and attributes assign() – loads a new document reload() – reloads the current document replace() – Replaces the current document with a new one Properties (attributes) host, hostname, href, pathname, port – hostname/port number/path/port/URL information protocol – protocol of the current URL hash, search – URL from the hash sign/question mark SE-2840 Dr. Mark L. Hornick

Main headaches with BOM Differences between browsers Implementations of respective BOM models and the functions or attributes supported by the respective objects Each new release of IE or Firefox has new features supported Not every feature available in one browser will be supported in the other Encouraging news: IE and Firefox are complying with standards more than ever Obsolete! As of 2013, the info on this page is obsolete! SE-2840 Dr. Mark L. Hornick

Part 2: The Document Object Model Note: The Document Object Model (DOM) is a child/subset of the BOM SE-2840 Dr. Mark L. Hornick

The Document Object Model (DOM) represents the objects that comprise a web page document What SE2811 pattern Does this remind you of? The hierarchical structure of the DOM below the <body> element depends on the current web page content SE-2840 Dr. Mark L. Hornick

All nodes have relationships to each other Every node has a parent except for the document node Most elements have descendant nodes document html head <p id=“par1”> Here is an<em id=“em1”>emphasized</em> word</p> Here is an emphasized word body title p My title Element node Here is an em word id=“par1” Text node emphasized id=“em1” Attribute node SE-2840 Dr. Mark L. Hornick

Looking closer at an Element object within the <body>: Text and Attribute descendents <p id=“par1”> Here is an<em id=“em1”>emphasized</em> word</p> Here is an emphasized word p Element Here is an em word Text id=“par1” Attribute emphasized id=“em1” SE-2840 Dr. Mark L. Hornick

Each Element, Attribute, or Text fragment in an HTML document is represented by a corresponding object in the DOM hierarchy The DOM objects are programmatically accessible via Javascript These objects are called nodes Nodes can be dynamically accessed, added, deleted, or modified Element node Text node p Attribute node Here is an em word id=“par1” Note: There is also a Comment node. emphasized id=“em1” SE-2840 Dr. Mark L. Hornick

The DOM implemented by modern web browsers allows you to change virtually anything on a webpage <p id=“par1”> Here is an<em id=“em1”>emphasized</em> word</p> Here is an emphasized word p Element Here is an em word Text id=“par1” Attribute emphasized id=“em1” Every element of a webpage can be accessed as a DOM object SE-2840 Dr. Mark L. Hornick

Some document methods and attributes for accessing various subnodes CS-321 4/8/2019 Some document methods and attributes for accessing various subnodes document Methods: getElementById(id) – gets the Element in the document with a specified id getElementsByTagName(tagName) – gets a collection of Elements with the specified tag name (e.g. “h1”) getElementsByClassName(className) – gets a collection of Elements with the specified class name (e.g. “highlights”) getElementByName() – gets a collection of Elements with the specified name (ex of named element: <form name=“form1"><input type="text"></form> ) querySelectorAll() – gets a collection of Elements with the specified selector pattern (e.g. “p em”) querySelector() – gets the first Element with the specified selector pattern (e.g. “p em”) 2013 Note: Seems to be limited support for this method (fails on FF) document Properties anchors[ ], forms[ ], images[ ], links[ ] – collections of all objects of this type within the current document body – the <body> Element object documentElement– the <html> Element object Note: The document object contains many other methods and properties for various other purposes – view them from within the browser debugger SE-2840 Dr. Mark L. Hornick Dr. Mark L. Hornick

JavaScript access to DOM nodes var pars = document.getElementsByTagName(“p”);//array of all <p> nodes var e = pars.item(0); // first <p> e = pars[0]; // also first <p> e = document.getElementById(“em1”);// specific element with #em1 id getElementByID is one of the easiest ways to access an individual element p Element node Here is an em word id=“par1” Text node emphasized id=“em1” Attribute node SE-2840 Dr. Mark L. Hornick

Navigating among nodes var p1 = document.getElementById(“par1”); // <p> node with id=“par1” var first = p1.firstChild; // first child node: “Here is an” var next = first.nextSibling; // next child: “em” var last = p1.lastChild; // last child: “word” var prev = last.prevSibling; // previous child: “em” var parent = p1.parentNode; // whatever element contained <p> Avoid; use ids! p Element node Here is an em word id=“par1” Text node emphasized id=“em1” Attribute node SE-2840 Dr. Mark L. Hornick

Besides changing the existing HTML markup within a node, we can script the DOM to create entirely new nodes var elm = document.createElement("p"); // creates free-standing <p> var txt = document.createTextNode("Hello"); elm.appendChild(txt); elm.setAttribute("id", “par1"); txt = document.createTextNode(" there."); document.body.appendChild(elm); // put in <body> body Complex; Avoid! p id=“par1” Hello there. <body> <p id=“par1”>Hello there.</p> </body> SE-2840 Dr. Mark L. Hornick

Getting/Setting the HTML within a node var p1 = document.getElementById(“par1”); // <p> node with id=“par1” var markup = p1.innerHTML; // “Here is an<em id=“black>emphasized</em> word” p1.innerHTML = “Here is some <em>new</em> text!”; Use this approach whenever possible to modify the content of an html element! p Element node Here is an em word id=“par1” Text node emphasized id=“em1” Attribute node SE-2840 Dr. Mark L. Hornick

The id or class of an existing element can be changed dynamically in order to modify the element’s appearance var elm = document.getElementById(“par1”); elm.setAttribute("id", “par2"); body <style type=“text/css”> #par1{ color:blue;} #par2{ color:red;} </style> … id=“par1” p <body> <p id=“par1”>Hello there.</p> </body> id=“par2” Hello there. <body> <p id=“par2”>Hello there.</p> </body> SE-2840 Dr. Mark L. Hornick

All modern browsers implement the element’s style object The style object contains the element’s CSS properties You can use the style object to change any CSS property the element supports Style property names generally correspond to CSS property names, with a few exceptions In the case of CSS property names that contain a hyphen, remove the hyphen and capitalize the next letter for the corresponding style property CSS property: “background-color” Style object property: “backgroundColor” SE-2840 Dr. Mark L. Hornick

Or style attributes can be modified directly via the style object <style type=“text/css”> #par1{ color:blue;} #par2 { color:red;} </style> … <script> var elm = document.getElementById(“par1”); elm.style.color=“green”; </script> body p id=“par1” The <p> element’s id remains “par1”, but the style object is more specific than the CSS rule, so the CSS rule is overridden. Hello there. SE-2840 Dr. Mark L. Hornick

The visibility and display style attributes affect whether the element is seen or not, but in different ways var elm = document.getElementById(“error”); // gets error message elm.style.display = “none”; // removes element from display process elm.style.display = “block”; // includes element elm.style.visibility = “hidden”; // hides elm.style.visibility = “visible”; // shows The display attribute, when set to “none”, causes the element to be removed from the display. Other elements will fill in the space left behind. The visibility attribute, when set to “hidden”, causes the element to be kept in the display, but not shown. An empty space will appear where the element would otherwise appear. This is a common approach to displaying or hiding web page error messages SE-2840 Dr. Mark L. Hornick