• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Make DOMParser use Standard mode instead of Quirks mode

Resolved: Make DOMParser use Standard mode instead of Quirks mode

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

Just realized that by default, DOMParser.parseFromString() creates documents in Quirks mode. Is there a way to make it Standard mode?

console.log(document.compatMode) // 'CSS1Compat'

const doc = new DOMParser().parseFromString('<div>Hello World</div>', 'text/html');
console.log(doc.compatMode) // 'BackCompat'

Answer:

All you need to do is prepend <!DOCTYPE html> to the HTML your parsing
DOMParser will result in the following “document”
<html>
  <head>
  </head>
  <body>
    <div>Hello World</div>
  </body>
</html>

console.log(document.compatMode) // 'CSS1Compat'

const doc = new DOMParser().parseFromString('<!DOCTYPE html><div>Hello World</div>', 'text/html');
console.log(doc.compatMode) // 'BackCompat'
console.log(doc.documentElement.outerHTML)

If you have better answer, please add a comment about this, thank you!

dom domparser html javascript
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Vaadin 14.9 – Redirect on Session Destroy Event

26/03/2023

Resolved: How to separate multiple answers in one column, for multiple columns, by creating extra columns

26/03/2023

Resolved: How to set consistent decimal separators in R data frame?

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.