Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda