SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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)
 

Ähnlich wie 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)
 

Kürzlich hochgeladen

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
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
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Kürzlich hochgeladen (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
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
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

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