SlideShare a Scribd company logo
1 of 18
Download to read offline
Document	
  Object	
  Model	
  and	
  
JavaScript	
  
Jussi	
  Pohjolainen	
  
Tampere	
  University	
  of	
  Applied	
  Sciences	
  
W3C	
  DOM	
  
•  DOM	
  –	
  Document	
  Object	
  Model	
  –	
  cross-­‐
plaDorm	
  and	
  language-­‐independent	
  
convenFon	
  for	
  interacFng	
  with	
  objects	
  in	
  
HTML	
  and	
  in	
  XML.	
  
•  With	
  DOM	
  you	
  can	
  manipulate	
  html/xml	
  
document!	
  Dynamic	
  html!	
  
•  Public	
  interface	
  available:	
  hPp://www.w3.org/
DOM/DOMTR	
  	
  
W3C	
  DOM	
  Levels	
  
•  (	
  DOM	
  Level	
  0	
  and	
  Intermediate	
  DOM	
  )	
  
–  Not	
  W3C	
  Standards,	
  used	
  in	
  Netscape	
  2	
  (Level	
  0)	
  and	
  
Netscape	
  4	
  (Intermediate	
  DOM)	
  	
  
•  DOM	
  Level	
  1	
  
–  1998:	
  Ability	
  to	
  change	
  enFre	
  HTML	
  or	
  XML	
  document	
  
•  DOM	
  Level	
  2	
  
–  2001:	
  Introduces	
  “getElementById”	
  funcFon,	
  event	
  
model	
  and	
  support	
  for	
  XML	
  namespaces	
  
•  DOM	
  Level	
  3	
  
–  2004:	
  XPath,	
  keyboard	
  event	
  handling	
  
Reality	
  
See	
  
hPp://www.quirksmode.org/dom/w3c_core.html	
  
DOM	
  Programming	
  Intro	
  
•  DOM	
  is	
  a	
  model	
  that	
  describes	
  how	
  all	
  
elements	
  in	
  an	
  html	
  page	
  are	
  related	
  to	
  
topmost	
  structure:	
  document	
  itself	
  
•  Can	
  influence	
  the	
  document	
  
– See:	
  hPp://www.w3.org/TR/REC-­‐DOM-­‐Level-­‐1/
introducFon.html	
  
•  Possible	
  to	
  read,	
  modify	
  and	
  delete	
  tags	
  
Node	
  
•  In	
  DOM,	
  each	
  object	
  is	
  Node	
  
•  In	
  this	
  
– <p>Hello</p>	
  
•  You	
  have	
  two	
  nodes	
  	
  
– 1)	
  element	
  node	
  -­‐	
  p	
  	
  
– 2)	
  text	
  node	
  -­‐	
  "Hello"	
  
•  Text	
  node	
  is	
  child	
  node	
  of	
  p	
  element.	
  P	
  
element	
  is	
  parent	
  node	
  of	
  the	
  text	
  node	
  
Node	
  Example	
  
<p>This is a <strong>paragraph</strong></p>
<p>
|
--------------
| |
This is a <strong>
|
|
paragraph
APribute	
  Node	
  
<a href=“http://www.tamk.fi”>TAMK</a>
<a> -----------------
| |
TAMK href
|
http://www.tamk.fi
Different	
  Nodes	
  
•  Element	
  node:	
  p,	
  img,	
  a	
  
•  Text	
  node:	
  sometext	
  
•  APribute	
  node:	
  src	
  
DOM	
  Level	
  1:	
  To	
  Access	
  DOM	
  tree	
  
•  X	
  can	
  be	
  some	
  node	
  
– var parent = x.parentNode;
– var firstchild = x.childNodes[0];
•  How	
  to	
  get	
  reference	
  to	
  x?	
  
Document	
  object	
  
Access	
  
var title =
document.firstChild.firstChild.lastChild;
// document.html.head.title
var title =
document.firstChild.childNodes[0].childNodes[
0];
Gelng	
  Element	
  Easier	
  Way	
  
var x =
document.getElementsByTagName(‘strong')[0]
var x = document.getElementById('hereweare');
	
  
Changing	
  the	
  Node	
  
// <a href=“” id=“someId”>Link</p>
var x =
document.getElementById(’someId');
x.firstChild.nodeValue = “Hello!”;
x.setAttribute(“href”, “http:/…”);
	
  
Inner	
  HTML	
  
// <a href=“” id=“someId”>Link</p>
var x = document.getElementById(’someId');
x.innerHTML = “Hello!”;
// innerHTML is NOT W3C Standard and it’s
// slower…
CreaFng	
  and	
  Removing	
  Nodes	
  
var x = document.createElement(’hr');
document.getElementById('inserthrhere').appendChild(x);
var node = document.getElementById('inserthrhere')
node.removeChild(node.childNodes[0]);
var x = document.createTextNode(’SomeText');
document.getElementById('hereweare').appendChild(x);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function change()
{
// Get list of ALL <h1> - elements
var listOfHeading1 = window.document.getElementsByTagName("h1");
// Find the first <h1> - element in the list
var heading1 = listOfHeading1[0];
// Get the child - element of the first <h1> - element (Text)
var text = heading1.firstChild;
// Replace the text
text.data = "Hello from JS!";
}
//]]>
</script>
</head>
<body>
<h1>Title</h1>
<input type="submit" onClick="change();" value="click!"/>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function change()
{
// Reference to the table - element
var table = document.getElementById("mytable");
var tr = document.createElement("tr"); // <tr>
var td1 = document.createElement("td"); // <td>
var td1Text = document.createTextNode("New Cell"); // "New Cell"
td1.appendChild(td1Text); // <td>New Cell</td>
var td2 = document.createElement("td"); // <td>
var td2Text = document.createTextNode("New Cell"); // "New Cell"
td2.appendChild(td2Text); // <td>New Cell</td>
tr.appendChild(td1);
tr.appendChild(td2);
table.appendChild(tr);
}
//]]>
</script>
</head>
<body>
<table id="mytable" border="1">
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
</table>
<input type="submit" onClick="change();" value="Add Row"/>
</body>
</html>

More Related Content

What's hot (20)

Dom
DomDom
Dom
 
Introduction to the DOM
Introduction to the DOMIntroduction to the DOM
Introduction to the DOM
 
INTRODUCTION TO DOM AND DOM TREE
INTRODUCTION TO DOM AND DOM TREEINTRODUCTION TO DOM AND DOM TREE
INTRODUCTION TO DOM AND DOM TREE
 
Document Object Model (DOM)
Document Object Model (DOM)Document Object Model (DOM)
Document Object Model (DOM)
 
Document object model(dom)
Document object model(dom)Document object model(dom)
Document object model(dom)
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
introduction to the document object model- Dom chapter5
introduction to the document object model- Dom chapter5introduction to the document object model- Dom chapter5
introduction to the document object model- Dom chapter5
 
Xml dom & sax by bhavsingh maloth
Xml dom & sax by bhavsingh malothXml dom & sax by bhavsingh maloth
Xml dom & sax by bhavsingh maloth
 
The Document Object Model
The Document Object ModelThe Document Object Model
The Document Object Model
 
DOM Quick Overview
DOM Quick OverviewDOM Quick Overview
DOM Quick Overview
 
Dom in javascript
Dom in javascriptDom in javascript
Dom in javascript
 
Introduction to DOM
Introduction to DOMIntroduction to DOM
Introduction to DOM
 
Dom structure
Dom structureDom structure
Dom structure
 
XML Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)
 
Html dom & j query
Html dom & j queryHtml dom & j query
Html dom & j query
 
Understanding XML DOM
Understanding XML DOMUnderstanding XML DOM
Understanding XML DOM
 
Understanding the dom by Benedict Ayiko
Understanding the dom by Benedict AyikoUnderstanding the dom by Benedict Ayiko
Understanding the dom by Benedict Ayiko
 
INFT132 093 07 Document Object Model
INFT132 093 07 Document Object ModelINFT132 093 07 Document Object Model
INFT132 093 07 Document Object Model
 
Web1O1 - Session 1
Web1O1 - Session 1Web1O1 - Session 1
Web1O1 - Session 1
 
Presentation
PresentationPresentation
Presentation
 

Viewers also liked

JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)Piyush Katariya
 
[SoftServe IT Academy] JavaScript Forms
[SoftServe IT Academy] JavaScript Forms[SoftServe IT Academy] JavaScript Forms
[SoftServe IT Academy] JavaScript FormsIvan Matiishyn
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom ManipulationMohammed Arif
 
DOM ( Document Object Model )
DOM ( Document Object Model )DOM ( Document Object Model )
DOM ( Document Object Model )ITSTB
 
JSP 빠르게 시작하기
JSP 빠르게 시작하기JSP 빠르게 시작하기
JSP 빠르게 시작하기Park JoongSoo
 

Viewers also liked (6)

Javascript and DOM
Javascript and DOMJavascript and DOM
Javascript and DOM
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
[SoftServe IT Academy] JavaScript Forms
[SoftServe IT Academy] JavaScript Forms[SoftServe IT Academy] JavaScript Forms
[SoftServe IT Academy] JavaScript Forms
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
 
DOM ( Document Object Model )
DOM ( Document Object Model )DOM ( Document Object Model )
DOM ( Document Object Model )
 
JSP 빠르게 시작하기
JSP 빠르게 시작하기JSP 빠르게 시작하기
JSP 빠르게 시작하기
 

Similar to JavaScript and DOM

WEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptxWEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptxkarthiksmart21
 
Basic web security model
Basic web security modelBasic web security model
Basic web security modelG Prachi
 
Web scraping with php
Web scraping with phpWeb scraping with php
Web scraping with phpChakrit Phain
 
Javascript dom event
Javascript dom eventJavascript dom event
Javascript dom eventBunlong Van
 
Dom date and objects and event handling
Dom date and objects and event handlingDom date and objects and event handling
Dom date and objects and event handlingsmitha273566
 
Modern Web UI - Web components
Modern Web UI - Web componentsModern Web UI - Web components
Modern Web UI - Web componentsMike North
 
04 sm3 xml_xp_07
04 sm3 xml_xp_0704 sm3 xml_xp_07
04 sm3 xml_xp_07Niit Care
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Modelchomas kandar
 
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSONAn introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSONSyed Moosa Kaleem
 
Web technologies-course 09.pptx
Web technologies-course 09.pptxWeb technologies-course 09.pptx
Web technologies-course 09.pptxStefan Oprea
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptSeble Nigussie
 

Similar to JavaScript and DOM (20)

lect9
lect9lect9
lect9
 
lect9
lect9lect9
lect9
 
WEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptxWEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptx
 
HTML_DOM
HTML_DOMHTML_DOM
HTML_DOM
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 
Basic web security model
Basic web security modelBasic web security model
Basic web security model
 
Web scraping with php
Web scraping with phpWeb scraping with php
Web scraping with php
 
Part 7
Part 7Part 7
Part 7
 
Javascript dom event
Javascript dom eventJavascript dom event
Javascript dom event
 
6867389.ppt
6867389.ppt6867389.ppt
6867389.ppt
 
6867389.ppt
6867389.ppt6867389.ppt
6867389.ppt
 
6867389 (1).ppt
6867389 (1).ppt6867389 (1).ppt
6867389 (1).ppt
 
Dom date and objects and event handling
Dom date and objects and event handlingDom date and objects and event handling
Dom date and objects and event handling
 
Modern Web UI - Web components
Modern Web UI - Web componentsModern Web UI - Web components
Modern Web UI - Web components
 
04 sm3 xml_xp_07
04 sm3 xml_xp_0704 sm3 xml_xp_07
04 sm3 xml_xp_07
 
Intro to Web Standards
Intro to Web StandardsIntro to Web Standards
Intro to Web Standards
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSONAn introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
 
Web technologies-course 09.pptx
Web technologies-course 09.pptxWeb technologies-course 09.pptx
Web technologies-course 09.pptx
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 

More from Jussi Pohjolainen

libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferencesJussi Pohjolainen
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationJussi Pohjolainen
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDXJussi Pohjolainen
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript DevelopmentJussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDXJussi Pohjolainen
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDXJussi Pohjolainen
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesJussi Pohjolainen
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platformJussi Pohjolainen
 

More from Jussi Pohjolainen (20)

Moved to Speakerdeck
Moved to SpeakerdeckMoved to Speakerdeck
Moved to Speakerdeck
 
Java Web Services
Java Web ServicesJava Web Services
Java Web Services
 
Box2D and libGDX
Box2D and libGDXBox2D and libGDX
Box2D and libGDX
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
 
libGDX: Tiled Maps
libGDX: Tiled MapslibGDX: Tiled Maps
libGDX: Tiled Maps
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame Animation
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDX
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript Development
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
libGDX: Scene2D
libGDX: Scene2DlibGDX: Scene2D
libGDX: Scene2D
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
libGDX: User Input
libGDX: User InputlibGDX: User Input
libGDX: User Input
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDX
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDX
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platform
 
Intro to Asha UI
Intro to Asha UIIntro to Asha UI
Intro to Asha UI
 

Recently uploaded

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 

JavaScript and DOM

  • 1. Document  Object  Model  and   JavaScript   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  • 2. W3C  DOM   •  DOM  –  Document  Object  Model  –  cross-­‐ plaDorm  and  language-­‐independent   convenFon  for  interacFng  with  objects  in   HTML  and  in  XML.   •  With  DOM  you  can  manipulate  html/xml   document!  Dynamic  html!   •  Public  interface  available:  hPp://www.w3.org/ DOM/DOMTR    
  • 3. W3C  DOM  Levels   •  (  DOM  Level  0  and  Intermediate  DOM  )   –  Not  W3C  Standards,  used  in  Netscape  2  (Level  0)  and   Netscape  4  (Intermediate  DOM)     •  DOM  Level  1   –  1998:  Ability  to  change  enFre  HTML  or  XML  document   •  DOM  Level  2   –  2001:  Introduces  “getElementById”  funcFon,  event   model  and  support  for  XML  namespaces   •  DOM  Level  3   –  2004:  XPath,  keyboard  event  handling  
  • 5. DOM  Programming  Intro   •  DOM  is  a  model  that  describes  how  all   elements  in  an  html  page  are  related  to   topmost  structure:  document  itself   •  Can  influence  the  document   – See:  hPp://www.w3.org/TR/REC-­‐DOM-­‐Level-­‐1/ introducFon.html   •  Possible  to  read,  modify  and  delete  tags  
  • 6. Node   •  In  DOM,  each  object  is  Node   •  In  this   – <p>Hello</p>   •  You  have  two  nodes     – 1)  element  node  -­‐  p     – 2)  text  node  -­‐  "Hello"   •  Text  node  is  child  node  of  p  element.  P   element  is  parent  node  of  the  text  node  
  • 7. Node  Example   <p>This is a <strong>paragraph</strong></p> <p> | -------------- | | This is a <strong> | | paragraph
  • 8. APribute  Node   <a href=“http://www.tamk.fi”>TAMK</a> <a> ----------------- | | TAMK href | http://www.tamk.fi
  • 9. Different  Nodes   •  Element  node:  p,  img,  a   •  Text  node:  sometext   •  APribute  node:  src  
  • 10. DOM  Level  1:  To  Access  DOM  tree   •  X  can  be  some  node   – var parent = x.parentNode; – var firstchild = x.childNodes[0]; •  How  to  get  reference  to  x?  
  • 12. Access   var title = document.firstChild.firstChild.lastChild; // document.html.head.title var title = document.firstChild.childNodes[0].childNodes[ 0];
  • 13. Gelng  Element  Easier  Way   var x = document.getElementsByTagName(‘strong')[0] var x = document.getElementById('hereweare');  
  • 14. Changing  the  Node   // <a href=“” id=“someId”>Link</p> var x = document.getElementById(’someId'); x.firstChild.nodeValue = “Hello!”; x.setAttribute(“href”, “http:/…”);  
  • 15. Inner  HTML   // <a href=“” id=“someId”>Link</p> var x = document.getElementById(’someId'); x.innerHTML = “Hello!”; // innerHTML is NOT W3C Standard and it’s // slower…
  • 16. CreaFng  and  Removing  Nodes   var x = document.createElement(’hr'); document.getElementById('inserthrhere').appendChild(x); var node = document.getElementById('inserthrhere') node.removeChild(node.childNodes[0]); var x = document.createTextNode(’SomeText'); document.getElementById('hereweare').appendChild(x);
  • 17. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" /> <script type="text/javascript"> //<![CDATA[ function change() { // Get list of ALL <h1> - elements var listOfHeading1 = window.document.getElementsByTagName("h1"); // Find the first <h1> - element in the list var heading1 = listOfHeading1[0]; // Get the child - element of the first <h1> - element (Text) var text = heading1.firstChild; // Replace the text text.data = "Hello from JS!"; } //]]> </script> </head> <body> <h1>Title</h1> <input type="submit" onClick="change();" value="click!"/> </body> </html>
  • 18. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" /> <script type="text/javascript"> //<![CDATA[ function change() { // Reference to the table - element var table = document.getElementById("mytable"); var tr = document.createElement("tr"); // <tr> var td1 = document.createElement("td"); // <td> var td1Text = document.createTextNode("New Cell"); // "New Cell" td1.appendChild(td1Text); // <td>New Cell</td> var td2 = document.createElement("td"); // <td> var td2Text = document.createTextNode("New Cell"); // "New Cell" td2.appendChild(td2Text); // <td>New Cell</td> tr.appendChild(td1); tr.appendChild(td2); table.appendChild(tr); } //]]> </script> </head> <body> <table id="mytable" border="1"> <tr><td>&nbsp;</td><td>&nbsp;</td></tr> <tr><td>&nbsp;</td><td>&nbsp;</td></tr> <tr><td>&nbsp;</td><td>&nbsp;</td></tr> </table> <input type="submit" onClick="change();" value="Add Row"/> </body> </html>

Editor's Notes

  1. See:http://en.wikipedia.org/wiki/Comparison_of_layout_engines_(Document_Object_Model)Trident: IE 4.0 -&gt;Tasman: IE For MacPresto: Opera