Dynamic HTML. A combination of technologies to create dynamic web pages – xhtml – CSS – Javascript Browsers make the page that is being displayed, its.

Slides:



Advertisements
Similar presentations
JavaScript – Quiz #8 Lecture Code:
Advertisements

The jQuery library. What is jQuery ? A javascript lightweight library Is very easy to use Is powerful Is cross-browser compatible Downloadable from jQuery.com,
CT-376 jQuery Most popular javascript library today Latest version:
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
JQuery A Javascript Library Hard things made eas(ier) Norman White.
CSS BASICS. CSS Rules Components of a CSS Rule  Selector: Part of the rule that targets an element to be styled  Declaration: Two or more parts: a.
CS7026 jQuery Events. What are Events?  jQuery is tailor-made to respond to events in an HTML page.  Events are actions that can be detected by your.
COMPSCI 345 / SOFTENG 350 TUTORIAL WEEK 8 | SAM KAVANAGH.
Week 12: JQuery Write less, do more.. JQuery Basics Lightweight JavaScript Library – Emphasizes interaction between JavaScript and HTML – Reduces technical.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Dynamic HTML. XHTML contentCSS style rules appearanceScriptingLanguage manipulate.
Philly.NET Hands-On jQuery + Plug-ins Bill Wolff, Rob Keiser.
JavaScript and The Document Object Model MMIS 656 Web Design Technologies Acknowledgements: 1.Notes from David Shrader, NSU GSCIS 2.Some material adapted.
1 Other Java Related Technologies. 2 HTML DOM 3 HTML DOM مجموعه ای استاندارد از اشیاء را برای HTML تعریف می کند و یک راه استاندارد برای دسترسی و دستکاری.
JQuery March 09 th,2009 Create by
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
JS: Document Object Model (DOM)
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
DOM and JavaScript Aryo Pinandito.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
JQUERY | INTRODUCTION. jQuery  Open source JavaScript library  Simplifies the interactions between  HTML document, or the Document Object Model (DOM),
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
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.
JavaScript, Part 3 Instructor: Charles Moen CSCI/CINF 4230.
5.2 DOM (Document Object Model). 2 Motto: To write it, it took three months; to conceive it three minutes; to collect the data in it — all my life. —F.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
Animation & Effects Using JQuery. What is jQuery? jQuery is a lightweight, JavaScript library. The purpose of jQuery is to make it much easier to use.
CHAPTER 5 jQuery อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
JQuery Introduction © Copyright 2014, Fred McClurg All Rights Reserved.
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 Library. What is jQuery jQuery is a lightweight JavaScript library. The purpose is to make it easier to use JavaScript code on your website.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
Web Programming Language Week 9 Dr. Ken Cosh Introducing jQuery.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
IS2802 Introduction to Multimedia Applications for Business Lecture 07: Introduction to jQuery Rob Gleasure
DOM (Document Object Model) - Parsing and Reading HTML and XML -
Host Objects: Browsers and the DOM
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
Introduction to JQuery COGS 187A – Fall JQuery jQuery is a JavaScript library, and allows us to manipulate HTML and CSS after the page has been.
CHAPTER 7 JQUERY WHAT IS JQUERY? jQuery is a script. It is written in JavaScript.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
JQuery Tutorial. What is jQuery jQuery is a JavaScript Library The purpose of jQuery is to make it much easier to use JavaScript on your website JavaScript.
Mobile Web Design Best Practices Three Approaches to Mobile Web: ◦ Develop a new mobile site with a.mobi TLD ◦ Create a separate website hosted within.
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.)
Dynamic HTML. 2 HTML contentCSS style rules appearanceScriptingLanguage manipulate.
Dynamic HTML. 2 HTML contentCSS style rules appearanceScriptingLanguage manipulate.
JQuery is a fast, small, and feature-rich javascript library. It makes things like HTML document traversal and manipulation, event handling, animation,
THE DOM.
Introduction to JavaScript Events
JavaScript Event Handling.
Introduction to Web programming
Document Object Model (DOM): Objects and Collections
© 2015, Mike Murach & Associates, Inc.
Javascript and JQuery SRM DSC.
E-commerce Applications Development
JavaScript and Events CS Programming Languages for Web Applications
Chengyu Sun California State University, Los Angeles
CHAPTER 6 EVENTS. CHAPTER 6 EVENTS WHAT IS AN EVENT?
JavaScript and Events CS Programming Languages for Web Applications
Presentation transcript:

Dynamic HTML

A combination of technologies to create dynamic web pages – xhtml – CSS – Javascript Browsers make the page that is being displayed, its style properties, and events accessible to JavaScript.

The DOM A platform- and language-neutral interface that allows programs and scripts to dynamically access and update the content and structure of HTML and XHTML documents every element in the HTML document is represented by an object Elements can be manipulated using the properties and methods of the corresponding objects Changes in the element properties are immediately reflected by the browser

The DOM

DOM Standards W3C defines the standards DOM Level 3 recommendation – DOM Level 2 HTML Specification – –additional DOM functionality specific to HTML, in particular objects for XHTML elements But, the developers of web browsers –don't implement all standards –implement some standards differently –implement some additional features

Accessing HTML Elements All HTML elements (objects) are accessed through the document object document itself is automatically created Several ways to access a specific element – paths in the DOM tree – retrieval by tag – retrieval by ID

Accessing Elements by Tags This example is lovely. But this one is even more! function execute() { var spans = document.getElementsByTagName("span"); spans[0].style.color="red"; spans[1].style.color="blue"; spans[1].style.fontVariant="small-caps"; } body head

Accessing Elements by ID This text can be hidden! function execute() { var theDiv = document.getElementById("div1"); if (theDiv.style.visibility=="hidden") {theDiv.style.visibility="visible" } else {theDiv.style.visibility="hidden" } } head This technique is more stable w.r.t. document changes (why?) body

Element Properties Elements of different types have different sets of properties and methods See for a detailed list of element properties and methodswww.w3schools.com/htmldom Most elements have the style member style is an object that represents the style- sheet rules applied over the element

Other Node Properties nodeType property ELEMENT_NODE : HTML element TEXT_NODE : text within a parent element ATTRIBUTE_NODE : an attribute of a parent element –attributes can be accessed another way CDATA_SECTION_NODE –CDATA sections are good for unformatted text – nodeName property – nodeValue property – attributes property – innerHTML property not standard, but implemented in major browsers very useful – style property object whose properties are all style attributes, e.g., those defied in CSS

The innerHTML Property The attribute innerHTML attribute of an element is the HTML code embedded inside that element Hence, you can replace existing content by setting this attribute: – element.innerHTML = "new HTML code” function replace(button) { d = document.getElementById("d1"); d.innerHTML = " This is a header "; button.disabled=true; }

Special DOM Objects window –the browser window –new popup window s can be opened document –the current web page inside the window body – element of the document history –sites that the user visited –makes it possible to go back and forth using scripts location –URL of the document –setting it goes to another page

Events

Event Example Simple Events function focusInput() { var theInput = document.getElementsByTagName("input")[0] theInput.style.background="yellow" } function blurInput() { theInput = document.getElementsByTagName("input")[0] theInput.style.background="white" }

Event Example (cont) <img src="lighton.gif" alt="light bulb" onmouseover="alert('Mouse Over')" /> <input type="text" onfocus="focusInput()" onblur="blurInput()" />

Event Model Events usually occur due to users actions – For example, pressing the keyboard, changing a text field, moving the mouse over an element, etc. An event is represented by an event object that is created upon the event occurrence Every event has an associated target element – For example, the image over which the mouse clicks

Event Model (cont) Elements can have registered event listeners which are associated with certain types of events When an event takes place, the listeners that are registered for this event are invoked Typically, a listener is described by a scripting code (e.g., JavaScript) – This code is executed upon listener invocation

Inline Listener Registration The simplest (and most common) way to register a listener is by an attribute assignment: ontype = "JavaScript code" For example: The JavaScript code has access to the following objects: – this - the element (e.g., the image defined above) – event - the event object

Some Event Types load unload abort click dblclick mousedown mousemove mouseup mouseover reset select submit change blur focus keydown keypress keyup

Another Example Event Object Example function execute(e) { alert(" x: " + e.clientX + ", y: " + e.clientY + " mouse button: " + e.button); } <body onmousedown="execute(event)" style="cursor: pointer; position:absolute; width:100%; height:100%">

JQuery

Open source library for manipulating the DOM JQuery object has functions for – Selecting DOM elements – Manipulating DOM elements – Event handling – Effects and Animations – AJAX – Utilities Simple way into dynamic HTML

jQuery Object jQuery – Most often seen as $ $ is a legal identifier in JavaScript – Better, cleaner to use jQuery Can be used as a function and as an object – JQuery function is used to select DOM elements – jQuery object is used for utility functions Includes a way to define a function that will run when the page is fully loaded

jQuery document ready Function that is called when page is loaded jQuery(document).ready(function() { // Javascript }); Can be placed anywhere

jQuery Hello World function tryMe() { jQuery("#buttondiv").html("You pushed my button"); } $(document).ready(function(){ $("#msgid").html("This is Hello World by JQuery"); }); This is Hello World by HTML Try Me

jQuery Selector Select DOM elements by – HTML element tagjQuery(“p”) – HTML element classjQuery(“.class”) – HTML element idjQuery(“#id”) – More advanced selectors Selector returns the elements as a JavaScript object – Methods for manipulating the elements

jQuery Selector $(selector) selector: – $( ‘ #id ’ )id of element – $( ‘ p ’ )tag name – $( ‘.class ’ )CSS class – $( ‘ p.class ’ ) elements having the CSS class – $( ‘ p:first ’ )$( ‘ p:last ’ )$( ‘ p:odd ’ )$( ‘ p:even ’ ) – $( ‘ p:eq(2) ’ )gets the 2 nd element (1 based) – $( ‘ p ’ )[1]gets the 2 nd element (0 based) – $( ‘ p:nth-child(3))gets the 3 rd element of the parent. n=even, odd too. – $( ‘ p:nth-child(5n+1) ’ )gets the 1 st element after every 5th one – $( ‘ p a ’ ) elements, descended from a – $( ‘ p>a ’ ) elements, direct child of a – $( ‘ p+a ’ ) elements, directly following a – $( ‘ p, a ’ ) and elements – $( ‘ li:has(ul) ’ ) elements that have at least one descendent – $( ‘ :not(p) ’ )all elements but elements – $( ‘ p:hidden ’ )only elements that are hidden – $( ‘ p:empty ’ ) elements that have no child elements

Useful jQuery Functions.each()iterate over the set.size()number of elements in set.end()reverts to the previous set.get(n)get just the nth element (0 based).eq(n)get just the nth element (0 based) also.lt(n) &.gt(n).slice(n,m)gets only nth to (m-1)th elements.not( ‘ p ’ )don ’ t include ‘ p ’ elements in set.add( ‘ p ’ )add elements to set.remove() removes all the elements from the page DOM.empty()removes the contents of all the elements.filter(fn/sel)selects elements where the func returns true or sel.find(selector)selects elements meeting the selector criteria.parent()returns the parent of each element in set.children()returns all the children of each element in set.next()gets next element of each element in set.prev()gets previous element of each element in set.siblings()gets all the siblings of the current element

Other Functions Formatting Functions –.css(property, value) –.html() –.val()(form elements) –.text() –.addClass( ‘ class ’ ) –.removeClass( ‘ class ’ ) Adding Elements – $( ‘ #target ’ ).before( ‘ Inserted before #target ’ ); – $( ‘ #target ’ ).after( ‘ This is added after #target ’ ); – $( ‘ #target ’ ).append( ‘ Goes inside #target, at end ’ ); – $( ‘ #target ’ ).wrap( ‘ ’ );

Example 1 Compare the following: What are the advantages of the jQuery method? $("a").click(function(){ alert("You clicked a link!"); }); $("a").click(function(){ alert("You clicked a link!"); }); Link

Example 2 $("h2").click(function(){ $(this).hide("slow"); }); What will this do? What happens if you have more than one h2? Try it! What will this do? What happens if you have more than one h2? Try it!

Example 3 $("#btnHideBlue").click(function(){ $("p.blue").hide("slow"); }); Hide all blue paragraphs when btnHideBlue is clicked Hide Blue

jQuery Events For a full jQuery event reference, please see jQuery Events ReferencejQuery Events Reference

Example 4 $("#lemon").mouseover(function(){ $(this).append(" Cookie! "); }); Append text to paragraph lemon on mouseover Lemon drops biscuit chocolate…

Manipulating CSS For a full jQuery CSS reference, please see jQuery CSS Methods ReferencejQuery CSS Methods Reference For more on the css function, see

Example 5 $("#btnColor").click(function(){ $("#lemon").addClass("blue"); }); Change color of paragraph lemon when btnColor is clicked.red{ color:red; }.blue{ color:blue; }.red{ color:red; }.blue{ color:blue; }

Example 6 Display the color of the paragraph lemon when btnColorCheck is clicked. What color is the paragraph? $("#btnColorCheck").click(function(){ alert($("#lemon").css("color")); });

Example 7 $("p").dblclick(function(){ $(this).css("background-color", "yellow"); }); Highlight (background-color = yellow) any paragraph that is double-clicked

jQuery Effects

Example 8 $("#btnToggle").click(function(){ $("#lemon").slideToggle("slow"); }); Create a toggle button that shows/hides paragraph lemon. Create a toggle button that shows/hides paragraph lemon.

Example 9 $("#btnFade").click(function(){ $("#lemon").fadeTo("slow", 0.5); }); Fade paragraph lemon to 50% opacity when btnFade is clicked.

Manipulating HTML For a full jQuery HTML reference, please see jQuery HTML Methods ReferencejQuery HTML Methods Reference

Example 10 $("#btnReplace").click(function(){ $("#lemon").html("Lollipop soufflé ice cream tootsie roll donut..."); }); Replace text in paragraph lemon when btnReplace is clicked.