Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Make DOMParser use Standard mode instead of Quirks mode

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'

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It looks like it will if you provide the full HTML markup including the standard doctype of <!DOCTYPE html>. If you only provide it an HTML fragment - like <div>Hello World</div> - rather than a full page, the engine will create a surrounding doctype and <html> and <body> for you, and will assume quirks mode.

console.log(document.compatMode);

const doc = new DOMParser().parseFromString(`
<!DOCTYPE html>
<html lang="en">
  <body>
    <div>Hello World</div>
  </body>
</html>
`, 'text/html');
console.log(doc.compatMode);

about 4 years ago · Juan Pablo Isaza Report

0

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!