SlideShare una empresa de Scribd logo
1 de 12
DOM The Document Object Model
Ia standard way of representing XML documents (instituted by the W3C). Was constructed to provide an intuitive way for developers to navigate an XML hierarchy. DOM is as a navigable tree, parents, children, siblings Every object in a DOM tree is a node. Each node can have a different type, such as element, text, or document What is dom?
<p><strong>Hello</strong> how are you doing?</p> Dom
Given <p><strong>Hello</strong> how are you doing?</p>. Given <p> find text “how are you doing”? Given <strong> find <p>? Given <strong> find “Hello” Given <strong> find “how are you doing?” Try Something Out My Dears.
Dom recognise line end as whitespace <html> <head> <title>Introduction to the DOM</title> </head> <body> <h1>Introduction to the DOM</h1> How if I want to get the title node? document.documentElement.firstChild.nexSibling.firstChild
function cleanWhitespace( element ) { // If no element is provided, do the whole HTML document element = element || document; // Use the first child as a starting point var cur = element.firstChild; // Go until there are no more child nodes while ( cur != null ) { // If the node is a text node, and it contains nothing but whitespace if ( cur.nodeType == 3 && ! /S/.test(cur.nodeValue) ) { // Remove the text node element.removeChild( cur ); // Otherwise, if it's an element } else if ( cur.nodeType == 1 ) { // Recurse down through the document 	cleanWhitespace( cur ); } cur = cur.nextSibling; // Move through the child nodes } } It is significantly slow, let say you inject a new node into document. Handling White Space in Dom
Element (nodeType = 1): This matches most elements in an XML file. For example, <li>,<a>, <p>, and <body> elements all have a nodeType of 1. Text (nodeType = 3): This matches all text segments within your document.  Document (nodeType = 9): This matches the root element of a document, <html> element. nodeType Property
Simple Dom Navigation function prev( elem ) { 	do { 		elem = elem.previousSibling; 	} while ( elem && elem.nodeType != 1 ); 	return elem; } function next( elem ) { 	do { 		elem = elem.nextSibling; 	} while ( elem && elem.nodeType != 1 ); 	return elem; } function first( elem ) { 	elem = elem.firstChild; 	return elem && elem.nodeType != 1?next ( elem ) : elem; } function last( elem ) { 	elem = elem.lastChild; 	return elem && elem.nodeType != 1 ? 	prev ( elem ) : elem; }
getElementById(“everywhere”): This method, which can only be run on the document object, finds all elements that have an ID equal to everywhere. This is a very powerful function and is the fastest way to immediately access an element. getElementsByTagName(“li”): This method, which can be run on any element, finds all descendant elements that have a tag name of li and returns them as a NodeList (which is nearly identical to an array). Dom Methods
function id(name) { 		return document.getElementById(name); 	} function tag(name, elem) { 		return (elem || document).getElementsByTagName(name); 	} Try Yourself
Getting the Text Inside an Element // Non-Mozilla Browsers: strongElem.innerText // All platforms: strongElem.firstChild.nodeValue
// Get the innerHTML of the <strong> element // Should return "Hello" strongElem.innerHTML // Get the innerHTML of the <p> element // Should return "<strong>Hello</strong> how are you doing?" pElem.innerHTML Getting the HTML Inside an Element

Más contenido relacionado

La actualidad más candente

JavaScript DOM & event
JavaScript DOM & eventJavaScript DOM & event
JavaScript DOM & eventBorey Lim
 
Document object model(dom)
Document object model(dom)Document object model(dom)
Document object model(dom)rahul kundu
 
Document object model
Document object modelDocument object model
Document object modelAmit kumar
 
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 chapter5FLYMAN TECHNOLOGY LIMITED
 
XML Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)BOSS Webtech
 
Dom(document object model)
Dom(document object model)Dom(document object model)
Dom(document object model)Partnered Health
 
Document Object Model
Document Object ModelDocument Object Model
Document Object ModelMayur Mudgal
 
HTML and XML Difference FAQs
HTML and XML Difference FAQsHTML and XML Difference FAQs
HTML and XML Difference FAQsUmar Ali
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XMLVijay Mishra
 
Lecture 4 - Adding XTHML for the Web
Lecture  4 - Adding XTHML for the WebLecture  4 - Adding XTHML for the Web
Lecture 4 - Adding XTHML for the Webphanleson
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XMLAbhra Basak
 

La actualidad más candente (20)

Dom
Dom Dom
Dom
 
JavaScript DOM & event
JavaScript DOM & eventJavaScript DOM & event
JavaScript DOM & event
 
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 Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)
 
Understanding XML DOM
Understanding XML DOMUnderstanding XML DOM
Understanding XML DOM
 
Dom(document object model)
Dom(document object model)Dom(document object model)
Dom(document object model)
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
Xml
XmlXml
Xml
 
DOM Quick Overview
DOM Quick OverviewDOM Quick Overview
DOM Quick Overview
 
Xml 215-presentation
Xml 215-presentationXml 215-presentation
Xml 215-presentation
 
HTML and XML Difference FAQs
HTML and XML Difference FAQsHTML and XML Difference FAQs
HTML and XML Difference FAQs
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Lecture 4 - Adding XTHML for the Web
Lecture  4 - Adding XTHML for the WebLecture  4 - Adding XTHML for the Web
Lecture 4 - Adding XTHML for the Web
 
Dhtml
DhtmlDhtml
Dhtml
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Markup Languages
Markup Languages Markup Languages
Markup Languages
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Dynamic HTML (DHTML)
Dynamic HTML (DHTML)Dynamic HTML (DHTML)
Dynamic HTML (DHTML)
 

Similar a The Document Object Model (20)

Xmlphp
XmlphpXmlphp
Xmlphp
 
Php Simple Xml
Php Simple XmlPhp Simple Xml
Php Simple Xml
 
XML
XMLXML
XML
 
XML
XMLXML
XML
 
XML
XMLXML
XML
 
Xml
XmlXml
Xml
 
Week4142
Week4142Week4142
Week4142
 
XML DTD Validate
XML DTD ValidateXML DTD Validate
XML DTD Validate
 
Xml intro1
Xml intro1Xml intro1
Xml intro1
 
Xml 150323102007-conversion-gate01
Xml 150323102007-conversion-gate01Xml 150323102007-conversion-gate01
Xml 150323102007-conversion-gate01
 
Xml material
Xml materialXml material
Xml material
 
Xml material
Xml materialXml material
Xml material
 
Xml material
Xml materialXml material
Xml material
 
Introduction to xml
Introduction to xmlIntroduction to xml
Introduction to xml
 
Douglas Crockford Presentation Jsonsaga
Douglas Crockford Presentation JsonsagaDouglas Crockford Presentation Jsonsaga
Douglas Crockford Presentation Jsonsaga
 
Kickstart Tutorial Xml
Kickstart Tutorial XmlKickstart Tutorial Xml
Kickstart Tutorial Xml
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
paper about xml
paper about xmlpaper about xml
paper about xml
 
Xml
XmlXml
Xml
 
Unit 5 xml (1)
Unit 5   xml (1)Unit 5   xml (1)
Unit 5 xml (1)
 

Último

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Último (20)

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

The Document Object Model

  • 1. DOM The Document Object Model
  • 2. Ia standard way of representing XML documents (instituted by the W3C). Was constructed to provide an intuitive way for developers to navigate an XML hierarchy. DOM is as a navigable tree, parents, children, siblings Every object in a DOM tree is a node. Each node can have a different type, such as element, text, or document What is dom?
  • 4. Given <p><strong>Hello</strong> how are you doing?</p>. Given <p> find text “how are you doing”? Given <strong> find <p>? Given <strong> find “Hello” Given <strong> find “how are you doing?” Try Something Out My Dears.
  • 5. Dom recognise line end as whitespace <html> <head> <title>Introduction to the DOM</title> </head> <body> <h1>Introduction to the DOM</h1> How if I want to get the title node? document.documentElement.firstChild.nexSibling.firstChild
  • 6. function cleanWhitespace( element ) { // If no element is provided, do the whole HTML document element = element || document; // Use the first child as a starting point var cur = element.firstChild; // Go until there are no more child nodes while ( cur != null ) { // If the node is a text node, and it contains nothing but whitespace if ( cur.nodeType == 3 && ! /S/.test(cur.nodeValue) ) { // Remove the text node element.removeChild( cur ); // Otherwise, if it's an element } else if ( cur.nodeType == 1 ) { // Recurse down through the document cleanWhitespace( cur ); } cur = cur.nextSibling; // Move through the child nodes } } It is significantly slow, let say you inject a new node into document. Handling White Space in Dom
  • 7. Element (nodeType = 1): This matches most elements in an XML file. For example, <li>,<a>, <p>, and <body> elements all have a nodeType of 1. Text (nodeType = 3): This matches all text segments within your document. Document (nodeType = 9): This matches the root element of a document, <html> element. nodeType Property
  • 8. Simple Dom Navigation function prev( elem ) { do { elem = elem.previousSibling; } while ( elem && elem.nodeType != 1 ); return elem; } function next( elem ) { do { elem = elem.nextSibling; } while ( elem && elem.nodeType != 1 ); return elem; } function first( elem ) { elem = elem.firstChild; return elem && elem.nodeType != 1?next ( elem ) : elem; } function last( elem ) { elem = elem.lastChild; return elem && elem.nodeType != 1 ? prev ( elem ) : elem; }
  • 9. getElementById(“everywhere”): This method, which can only be run on the document object, finds all elements that have an ID equal to everywhere. This is a very powerful function and is the fastest way to immediately access an element. getElementsByTagName(“li”): This method, which can be run on any element, finds all descendant elements that have a tag name of li and returns them as a NodeList (which is nearly identical to an array). Dom Methods
  • 10. function id(name) { return document.getElementById(name); } function tag(name, elem) { return (elem || document).getElementsByTagName(name); } Try Yourself
  • 11. Getting the Text Inside an Element // Non-Mozilla Browsers: strongElem.innerText // All platforms: strongElem.firstChild.nodeValue
  • 12. // Get the innerHTML of the <strong> element // Should return "Hello" strongElem.innerHTML // Get the innerHTML of the <p> element // Should return "<strong>Hello</strong> how are you doing?" pElem.innerHTML Getting the HTML Inside an Element