JavaScript APIs You’ve Never Heard Of (And some you have) Nicholas C.

Slides:



Advertisements
Similar presentations
The Document Object Model
Advertisements

5.1 JavaScript Execution Environment
Bring Life to Your Web Pages with JavaScript and HTML5 Ole Ildsgaard Hougaard -
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,
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
The Document Object Model (DOM) 1 JavaScript is an object-based language—that is, it’s based on manipulating objects by changing each object’s properties.
JQuery CS 380: Web Programming. What is jQuery? jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling,
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
1 JavaScript Document Object Model & Events. 2 Document Object Model (DOM) JavaScript access to the elements of an HTML document. An object hierarchy.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Sen Wang 11/17/2011.  RFC  “Form-based File Upload in HTML” NOV 1995 
CSE 154 LECTURE 24: XML AND JSON. Debugging responseXML in Firebug can examine the entire XML document, its node/tree structure.
PhpXperts What is jQuery Javascript Library Fast and concise Simplifies the interaction between HTML and JavaScript.
Internet Applications Spring Review Last week –MySQL –Questions?
Lecture 12 – AJAX SFDV3011 – Advanced Web Development Reference: 1.
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)
AJAX Without the “J” George Lawniczak. What is Ajax?
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
Chapter 5 © 2012 by Addison Wesley Longman, Inc JavaScript Execution Environment - The JavaScript Window object represents the window in which the.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
D2L Notes Be sure to submit your link in the dropbox provided on D2L You can just upload an empty text file if a file upload is required Do not use D2L.
Working with the XML Document Object Model ©NIITeXtensible Markup Language/Lesson 7/Slide 1 of 44 Objectives In this lesson, you will learn to: *Identify.
Client side web programming Introduction Jaana Holvikivi, DSc. School of ICT.
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.
JavaScript, Fourth Edition
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
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.
JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.
AJAX (also known as: XMLHTTP, Remote Scripting, XMLHttpRequest, etc.) Matt Warden.
XP Tutorial 16 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
Unleash the Power of jQuery Doncho Minkov Telerik Software Academy academy.telerik.com Senior Technical Trainer
DOM Document Object Model (DOM) SoftUni Team Technical Trainers Software University
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
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.
Introduction to the Document Object Model Eugenia Fernandez IUPUI.
Unleash the Power of jQuery Learning & Development Team Telerik Software Academy.
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
EIW: Javascript DOM and Events1 JavaScript Document Object Model & Events.
DOM (Document Object Model) - Parsing and Reading HTML and XML -
AJAX and REST. Slide 2 What is AJAX? It’s an acronym for Asynchronous JavaScript and XML Although requests need not be asynchronous It’s not really a.
Create Element, Remove Child. The Document Tree Document Element Root Element Element Element Element Element Text: HelloWorld Attribute “href”
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.
CSE 154 LECTURE 9: THE DOM TREE. The DOM tree The elements of a page are nested into a tree-like structure of objects the DOM has properties and methods.
XML DOM Week 11 Web site:
DOM Dr. Reda Salama. Back to the HTML DOM Once we get a response back from the server, we probably want to update our HTML page The HTML page itself is.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
JQUERY Dr Mohd Soperi Mohd Zahid Semester /16.
Introduction to JavaScript DOM Instructor: Sergey Goldman.
XP Tutorial 10 New Perspectives on JavaScript, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
THE DOM.
CHAPTER 5 DOCUMENT OBJECT MODEL
Scripting the DOM MIS 3502, Fall 2016 Jeremy Shafer Department of MIS
CGS 3066: Web Programming and Design Spring 2017
Introduction to the Document Object Model
DOM Robin Burke ECT 360.
AJAX and REST.
Document Object Model (DOM): Objects and Collections
Week 11 Web site: XML DOM Week 11 Web site:
Working with Dynamic Content and Styles
Chengyu Sun California State University, Los Angeles
Presentation transcript:

JavaScript APIs You’ve Never Heard Of (And some you have) Nicholas C.

Who’s this guy?

flickr.com/photos/vbillings/ /

HTML5, DOM4 & …DOM Level 2!

Item 1 UL LI #text

children Item 1 var list = document.getElementById(“mylist”); console.log(list.childNodes.length); //7 console.log(list.children.length); //3 DOM4 HTMLCollection of all child nodes that are elements No vendor prefix!

children Item 1 var list = document.getElementById(“mylist”), node1 = list.childNodes[0], child1 = list.children[0]; console.log(node1.nodeName); //”#text” console.log(child1.nodeName); //”LI” DOM4 HTMLCollection of all child nodes that are elements No vendor prefix!

children Item 1 var list = document.getElementById(“mylist”), node1 = list.childNodes[0], child1 = list.children[0]; console.log(node1.nodeName); //”#text” console.log(child1.nodeName); //”#comment” DOM4 HTMLCollection of all child nodes that are elements BUG! IE 6-8 includes comments in the children collection

Element Traversal API Defines new properties for accessing element children UL LI #text lastChildfirstChild

Element Traversal API Defines new properties for accessing element children UL LI #text lastElementChildfirstElementChild 9 No vendor prefix!

firstElementChild Item 1 var list = document.getElementById(“mylist”), node1 = list.firstChild, child1 = list.firstElementChild; console.log(node1.nodeName); //”#text” console.log(child1.nodeName); //”LI” Element Traversal API & DOM4 Point to first child node that is an element 9 No vendor prefix!

lastElementChild Item 1 var list = document.getElementById(“mylist”), node1= list.lastChild, child= list.lastElementChild; console.log(node1.nodeName); //”#text” console.log(child1.nodeName); //”LI” Element Traversal API & DOM4 Point to last child node that is an element 9 No vendor prefix!

Element Traversal API Defines new properties for accessing element children LI #text nextSibling 9

Element Traversal API Defines new properties for accessing element children LI #text nextElementSibling 9

Element Traversal API Defines new properties for accessing element children LI #text previousSibling 9

Element Traversal API Defines new properties for accessing element children LI #text previousElementSibling 9

// iterate over element children var child = element.firstElementChild; while(child) { process(child); child = child.nextElementSibling; } 9 Element Traversal API Defines new properties for accessing element children No vendor prefix!

contains() var element = document.getElementById(“foo”); if (document.body.contains(element)) { //do something } function isAncestor(child, maybeAncestor) { return maybeAncestor.contains(child); } // useful for event delegation if (isAncestor(event.target, list)) { // do something } DOM4 Determines if a given element is an ancestor of another No vendor prefix!

insertAdjacentHTML() element.insertAdjacentHTML(location, html); HTML5 Insert an HTML string into the DOM at a specific place “beforebegin” “afterbegin” “beforeend” “afterend” “beforebegin” “afterbegin” “beforeend” “afterend” Any valid HTML string No vendor prefix!

insertAdjacentHTML() Site Menu Home About var menu = document.getElementById("menu"); HTML5 Insert an HTML string into the DOM at a specific place No vendor prefix!

insertAdjacentHTML() Site Menu Hello world! Home About var menu = document.getElementById("menu"); menu.insertAdjacentHTML("beforebegin", " Hello world! "); HTML5 Insert an HTML string into the DOM at a specific place No vendor prefix!

insertAdjacentHTML() Site Menu Hello world! Home About var menu = document.getElementById("menu"); menu.insertAdjacentHTML(“afterbegin", " Hello world! "); HTML5 Insert an HTML string into the DOM at a specific place No vendor prefix!

insertAdjacentHTML() Site Menu Home About Hello world! var menu = document.getElementById("menu"); menu.insertAdjacentHTML(“beforeend", " Hello world! "); HTML5 Insert an HTML string into the DOM at a specific place No vendor prefix!

insertAdjacentHTML() Site Menu Home About Hello world! var menu = document.getElementById("menu"); menu.insertAdjacentHTML(“afterend", " Hello world! "); HTML5 Insert an HTML string into the DOM at a specific place No vendor prefix!

insertAdjacentHTML() HTML5 Insert an HTML string into the DOM at a specific place No vendor prefix! In many cases, faster than innerHTML!

outerHTML element.outerHTML = html; HTML5 Get/set HTML for an entire element Any valid HTML string No vendor prefix!

outerHTML Site Menu Home About var menu = document.getElementById("menu"); var html = menu.outerHTML; HTML5 Get/set HTML for an entire element No vendor prefix!

outerHTML Site Menu Hello world! var menu = document.getElementById("menu"); menu.outerHTML = " Hello world! "; console.log(menu.tagName); // “UL” console.log(menu.parentNode); // null HTML5 Get/set HTML for an entire element No vendor prefix! Detached reference to

createHTMLDocument() document.implementation.createHTMLDocument(title); DOM Level 2 Create an invisible document No vendor prefix! 9 Title of the document

createHTMLDocument() var doc = document.implementation.createHTMLDocument(“Test”); console.log(doc.title); // “Test” doc.body.innerHTML = “ Hello world! ”; var p = document.querySelector(“p”); console.log(p.textContent); // “Hello world!” DOM Level 2 Create an invisible document No vendor prefix! 9

createHTMLDocument() function isSafeHTML(html) { var doc = document.implementation.createHTMLDocument(“Test”); doc.body.innerHTML = html; return !doc.querySelector(“script,style,link,object”); } DOM Level 2 Create an invisible document No vendor prefix! 9

createHTMLDocument() function sanitizeHTML(html) { var doc = document.implementation.createHTMLDocument(“Test”); doc.body.innerHTML = html; var nodes = doc.querySelectorAll(“script,style,link,object”); for (var i=0, len=nodes.length; i < len; i++) { nodes[i].parentNode.removeChild(nodes[i]); } return doc.body.innerHTML; } DOM Level 2 Create an invisible document No vendor prefix! 9

setSelectionRange() HTML5 Select specific parts of textbox content No vendor prefix! var textbox = document.getElementById("data-field"); textbox.focus(); textbox.select(); textbox.setSelectionRange(1, 3); 9

setSelectionRange() HTML5 Select specific parts of textbox content No vendor prefix! // put caret at start textbox.setSelectionRange(0, 0); // put caret at end textbox.setSelectionRange( textbox.value.length, textbox.value.length); 9

selectionStart/selectionEnd HTML5 Set/get the start and ending range of selection No vendor prefix! var textbox = document.getElementById("data-field"); textbox.focus(); textbox.setSelectionRange(1, 3); console.log(textbox.selectionStart); // 1 console.log(textbox.selectionEnd); // 3 9

activeElement HTML5 Returns the element that currently has focus No vendor prefix! var textbox = document.getElementById("data-field"); textbox.focus(); var focused = document.activeElement; console.log(focused === textbox); // true

XMLHttpRequest Level 2

FormData var data = new FormData(); data.append(“name”, “Nicholas”); data.append(“age”, 25); data.append(“note”, “Yeah right!”); var xhr = new XMLHttpRequest(); xhr.open(“post”, “/submit”, true); //setup event handlers xhr.send(data); XMLHttpRequest Level 2 Used to submit data via XMLHttpRequest 10 3 No vendor prefix!

FormData var data = new FormData(document.forms[0]); var xhr = new XMLHttpRequest(); xhr.open(“post”, “/submit”, true); //setup event handlers xhr.send(data); XMLHttpRequest Level 2 Used to submit data via XMLHttpRequest 10 3 No vendor prefix!

FormData var data = new FormData(), fileControl = document.getElementById("photo"); data.append(“name”, “Nicholas”); data.append(“photo”, fileControl.files[0]); var xhr = new XMLHttpRequest(); xhr.open(“post”, “/submit”, true); //setup event handlers xhr.send(data); XMLHttpRequest Level 2 Used to submit data via XMLHttpRequest 10 3 No vendor prefix!

Upload Progress var xhr = new XMLHttpRequest(); xhr.open(“post”, “/submit”, true); xhr.upload.onprogress = function(event) { var percentage = event.loaded/event.total * 100; updateProgress(percentage); }; xhr.send(data); XMLHttpRequest Level 2 Monitor the time to upload 10 3 No vendor prefix!

XHR Timeouts var xhr = new XMLHttpRequest(); xhr.open(“get”, “/data”, true); xhr.timeout = 5000; xhr.ontimeout = function() { console.log(“Request timed out.”); }; // other event handlers xhr.send(data); XMLHttpRequest Level 2 Used to stop a request after a period of time 9 3 No vendor prefix!

XHR Response Types XMLHttpRequest Level 2 Retrieve a particular type of object – not just text! var xhr = new XMLHttpRequest(); xhr.open(“get”, “/data”, true); xhr.onload = function() { var text = xhr.responseText; doSomethingWith(text); }; // other event handlers xhr.send(); No vendor prefix!

XHR Response Types XMLHttpRequest Level 2 Retrieve a particular type of object – not just text! var xhr = new XMLHttpRequest(); xhr.open(“get”, “/data”, true); xhr.onload = function() { var xmldoc = xhr.responseXML; doSomethingWith(xmldoc); }; // other event handlers xhr.send(); No vendor prefix!

XHR Response Types XMLHttpRequest Level 2 Retrieve a particular type of object – not just text! 10 var xhr = new XMLHttpRequest(); xhr.open(“get”, “/data”, true); xhr.responseType = "text"; xhr.onload = function() { var text = xhr.response; doSomethingWith(text); }; // other event handlers xhr.send(); 3 No vendor prefix!

XHR Response Types XMLHttpRequest Level 2 Retrieve a particular type of object – not just text! 10 var xhr = new XMLHttpRequest(); xhr.open(“get”, “/data”, true); xhr.responseType = "document"; xhr.onload = function() { var xmldoc = xhr.response; doSomethingWith(xmldoc); }; // other event handlers xhr.send(); 3 No vendor prefix!

XHR Response Types XMLHttpRequest Level 2 Retrieve a particular type of object – not just text! 10 var xhr = new XMLHttpRequest(); xhr.open(“get”, “/data”, true); xhr.responseType = "blob"; xhr.onload = function() { var blob = xhr.response; doSomethingWith(blob); }; // other event handlers xhr.send(); Great for downloading images! 3 No vendor prefix!

XHR Response Types XMLHttpRequest Level 2 Retrieve a particular type of object – not just text! 10 var xhr = new XMLHttpRequest(); xhr.open(“get”, “/data”, true); xhr.responseType = "arraybuffer"; xhr.onload = function() { var binData = new Uint16Array(xhr.response); doSomethingWith(binData); }; // other event handlers xhr.send(); Great for downloading binary data! 3 No vendor prefix!

XHR Response Types XMLHttpRequest Level 2 Retrieve a particular type of object – not just text! var xhr = new XMLHttpRequest(); xhr.open(“get”, “/data”, true); xhr.responseType = "json"; xhr.onload = function() { var json = xhr.response; doSomethingWith(json); }; // other event handlers xhr.send(); Future

CSS in JavaScript

matchesSelector() var element = document.getElementById(“foo”); if (element.matchesSelector(“#foo”)) { //do something } if (element.matchesSelector(“body.bar”)) { //do something } Selector API Level 2 Determines if the element matches a certain CSS selector 9 3

matchesSelector() element.mozMatchesSelector() element.webkitMatchesSelector() element.msMatchesSelector() element.oMatchesSelector() Selector API Level 2 Determines if the element matches a certain CSS selector

matches () var element = document.getElementById(“foo”); if (element.matches(“#foo”)) { //do something } if (element.matches(“.bar”, element.parentNode)) { //do something } Selector API Level 2 Determines if the element matches a certain CSS selector Future

getBoundingClientRect() CSS Object Model Views Determines size and location of an element in the viewport Hello!

getBoundingClientRect() CSS Object Model Views Determines size and location of an element in the viewport var rect = element.getBoundingClientRect(); // all measurements in pixels relative to viewport console.log(rect.left); console.log(rect.top); console.log(rect.right); // relative to left console.log(rect.bottom); // relative to top console.log(rect.width); console.log(rect.height); No vendor prefix!

getBoundingClientRect() CSS Object Model Views Determines size and location of an element in the viewport var rect = element.getBoundingClientRect(); // all measurements in pixels relative to viewport console.log(rect.left); console.log(rect.top); console.log(rect.right); // relative to left console.log(rect.bottom); // relative to top console.log(rect.width); console.log(rect.height); BUG! IE < 8 adds 2 to each coordinate – you must subtract it

elementFromPoint() var element = document.elementFromPoint(x, y); CSS Object Model Views Return the element at a position relative to viewport Think clientX and clientY No vendor prefix! Element at that point with highest z-index

elementFromPoint() var element = document.elementFromPoint(x, y); CSS Object Model Views Return the element at a position relative to viewport Think clientX and clientY No vendor prefix! Element at that point with highest z-index

matchMedia() var mql = window.matchMedia(“(max-width:600px)”); if (mql.matches) { //do something } mql.addListener(function(mql) { console.log(mql.media + “ “ + (mql.matches ? “matches” : “doesn’t match”); });}); CSS Object Model Views Allows JavaScript to interact with CSS media queries 10 No vendor prefix!

Review

What We Talked About Element Traversal API element.children element.contains() element.insertAdjacentHTML() element.outerHTML document.activeElement document.implementation.createHTMLDocument() element.setSelectionRange() element.selectionStart element.selectionEnd

What We Talked About FormData Upload Progress XHR Timeouts XHR Response Types element.matchesSelector() element.getBoundingClientRect() document.elementFromPoint() window.matchMedia()

The End

Etcetera My blog: nczonline.net These Slides:slideshare.net/nzakas