63
[email protected] Introduction to HTML / JavaScript / DOM This document: http://arnaud-nauwynck.github.io/docs/Intro-Html-JS- DOM.pdf

Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Embed Size (px)

Citation preview

Page 1: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

[email protected]

Introduction to HTML / JavaScript / DOM

This document:http://arnaud-nauwynck.github.io/docs/Intro-Html-JS-

DOM.pdf

Page 2: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Agenda

● Introduction to Client-Side Web Technologies– HTML = Elements / Custom Elements

– CSS

– JavaScript

● The DOM … how JavaScript interact

● JavaScript framework(s)– AngularJS, binding principles

Page 3: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

21 Billions...

Rank#1 : w3scools (not Wikipedia?)

Page 4: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model
Page 5: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

HyperText Markup Langage

Page 6: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

1989 : HTTP + HTMLTim Berners-Lee

Page 7: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

HyperText Markup Language (HTML) is the standard markup language for creating web pages and web applications.

Page 8: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

With Cascading Style Sheets (CSS), and JavaScript, it forms a triad of cornerstone technologies for the World Wide Web

WWWW3C Technologies =

HTML

CSS JavaScript

Page 9: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Client(Html+CSS+JS) – Protocol(Http) - Server(*)

HTML

CSS JavaScript

HTTP Request (GET,POST,..) URLheadersbody

HTTP Response (20x,30x,40x,50x)headersbody

Client-Side

WebSocket events

80808443

Server-Sideexpose IP<TCP<HTTP port

Page 10: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Client-Side = Browser= Security Sandbox

HTML

CSS JavaScript https://site1

https://site2

https://site3

Cookie / LocalStorage site1.com

Cookie site2.com

Cookie site3.com

Page 11: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Site Navigation Isolation …

Example on Chrome:

1 separate Process per navigation Tab !!!

Shared processes(chrome launcher)

Page 12: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

On Client-Side = ONLY Html / CSS / (Limited) JavaScript

HTML

CSS JavaScript

JavaScript access to global “Window” (current frame)

NO access to C:\*, D:\*only “Save As” to D:\Downloads

+ OpenFileDialog for upload

W3C API for Mouse (swipe..)

ScrollbarKeyBoard

Micro...

Amazing WebGL, Canvas, SVG renderingusing video card !!

JS limited to 2 Threadsand Few Connections

per site

Page 13: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

1/ HTML

2/ CSS 3/ JavaScript

1/ HTML ~ XML elements for Hyper Textcontains Data

Page 14: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Element = <tagName> ... </tagName>

Page 15: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

<elt attribute1=”value1” attribute2 ../>

Page 16: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Standard W3c Tags

<h1>Header 1</h1><h2>..</h2><h3>..<h3><h4>..</h4><p>paragraph</p><br/> <pre> - pre-formatted

<b> - Bold text<strong> - Important text<i> - Italic text<em> - Emphasized text<mark> - Marked text<small> - Small text<del> - Deleted text<ins> - Inserted text<sub> - Subscript text<sup> - Superscript text

Text Formatting

Basic Structure<A href=”..”><img src=”..”><video ><script ><iframe>

Hyper Link..

<table> <tr><td><ul> <li><><>...…...

Other..

Page 17: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Html 1.0 … a simple MarkupLangage for Text

=> many specific tags for text formatting

Page 18: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Only 2 families of tag layout: Blocks & Inline

=> technically no “needs” to add more than <div>&<span>

with CSS & JS everywhere

Page 19: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

<div> and <span> ..

<div> = blocks <span> = inline

Page 20: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Html = soup of <div><div><div> (example: gmail)

Page 21: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Code quality with Only technical <div>&<span> ?

Need more semantic tags for web applications

Page 22: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

CustomElement

Define your own application specific tags: <your-custom-tag>

<your-menu> <your-view> <your-object>

Page 23: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Eric Bidelman's articlehttps://www.html5rocks.com/en/tutorials/webcomponents/

customelements/

Page 24: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model
Page 25: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Example Demo : http://html5-demos.appspot.com/hangouts

Page 26: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model
Page 27: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Custom Elt

Page 28: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Custom Elements ...

Page 29: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

WebComponent… Simplify CustomElement

Goals :

1) simplify Javascript ...replace by Html <dom-module><template>

2) supports old browser via PolyFill

3) use Template + ShadowDom

Page 30: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model
Page 31: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

WebComponents = 4 w3c specs = CustomElement + Import + Template + ShadowDom

Page 32: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

www.polymer-project.org= Google Library of WebComponents

Page 33: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Polymer example

File “my-element.html”

Page 34: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Polymer custom elements catalog

Page 35: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Polymer Core Elements : Fe (Iron)

Page 36: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Iron example<iron-swipeable-container>

https://github.com/PolymerElements/Iron-swipeable-container/blob/master/demo/index.html

Page 37: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Material Design : Md (Paper)

Page 38: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Md Paper Example

Page 39: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

1/ HTML

2/ CSS 3/ JavaScript

2/ CSS ~ size,border,colors,fontscontains Style

Page 40: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

style=” …inlined CSS property:value;”

Page 41: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Styles … using CSS files

Page 42: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Internal / External CSS

Page 43: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Style with < class=”..”>

Page 44: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

1/ HTML

2/ CSS 3/ JavaScript

3/ Scriptcontains actions,events,behavior

Page 45: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model
Page 46: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Html / CSS / JavaScript Interactions

1/ HTML

2/ CSS 3/ JavaScript

Affect renderer

<style>..</style>attr style=””<link rel="stylesheet"

Link <script>onDocumentLoadonClickonXX events

New Add Remove Setelement/attr(=DOM )

Css updateAdd/remove class attribute(=DOM )

Page 47: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

DOM = Document Object Model

Document

URL, ..

Node(Abstract class)

Element Attribute Text Comment

Body

Header

<H1> <div>

<p>

<A href >

<span>

<fe-*>, <paper-*>,

<your-custom-element>

0..*childNodes

0..*attr

0..1parent

Window

Page 48: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

DOM = Class Hierarchy

public abstract class Node { private Node parent; .. public Node getParent() { .. }}

public abstract class Element extends Node { private List<Node> childList; private List<Attribute> attributes; ..}

Page 49: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

DOM Reference

Page 50: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

DOM Elements Properties,Method,Events

Page 51: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

DOM Elements Reference

Page 52: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Typical JS Code to Find & Edit Elts

Find:

Document .getElementById()

Create:

Document .createElement(“li”) .createTextNode()...

Update:

element.appendChild()value = element.attributeelement.attribute = valueElement.innerHTML...

Page 53: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

JQuery for Portable/ConciseDOM Manipulation?

Make DOM manipulation,event,... “simpler” ...

Page 54: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Same TodoItem example using JQuery

Page 55: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Jquery $() : wrapper for Document.getElementBy..(..)

Code is smaller ?? by including “<script src=”jquery.min.js”></script>

Well ... If you use only 1 method of Jquery...

You could have use “jqueryLight”

or evenfunction my$(id) { return Document.getElementById(id);}

Page 56: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Jquery Risk… Script Code Injection!

Example in previous example : append(“<li>” + text + “</li>”) … text can contain html script!!

Page 57: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Jquery vs DOMNaive retrial after google it

… same pb !!

This one works ...

Compare with Explicit DOM + findById

Page 58: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Jquery Bashing in 2016 ...

$() code not simple ! … not “typed”

JQuery is a very low-level framework … for writing mostly imperative codeOr applying plugins

No easy bi-direction BindingBetter use Richer Framework, like AngularJS

Browser Portability no more an issue in 2016?

Page 59: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model
Page 60: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Same TodoItem example using AngularJS-1.x

Page 61: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

DOM vs Jquery vs AngularJS

● AngularJS code ~ 25 lines / jquery ~ 21 lines● AngularJS code much more readable

Declarative code

MVC : Model-View-Controller

Bi-Directionnal Binding

(no events required, only Model)

Imperative code

Direct DOM manipulation

NO Binding, Events

events history required

Page 62: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Much more to say on Angular MVC Binding...

● very handy writing quick web app● not very efficient having 1000 objects in scope● ...digest() loop is horrible

● Angular 2.x is complete rewrite of 1.x● ...still not very clean code (digest zone?)

Model should be controlled.. and fire chg Event

● ReactJs, Aurelia, ..

… JS Word = “1 fwk a day, the doctor away”?

Page 63: Introduction to HTML / JavaScript / DOM - Arnaud …arnaud-nauwynck.github.io/docs/Intro-Html-JS-DOM.pdf · Introduction to HTML / JavaScript / DOM ... DOM = Document Object Model

Conclusion

● Only a “very short” introduction to Html-Js-Dom

● I Hope you feel– “I Did not know all this before”

– “I am happy I have learned something”

– “I want to learn more”

This document:http://arnaud-nauwynck.github.io/docs/Intro-Html-JS-

DOM.pdf