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

238
Vistas
How to disable parsing code in script tags using JavaScript DOMParser?

I have the following text file with JavaScript tags:

<script>
...
</script>
<script>
...
</script>
<script>
...
</script>

when I parse it like so:

const parser = new DOMParser();
const xmlDoc = parser.parseFromString(`<root>${data}</root>`, "text/xml");
const tags = xmlDoc.getElementsByTagName("script");

I get an error because there are < and > symbols in JS program code, like

for (let i = 0; i < tags.length; i++) {

I don't want to replace < > with &lt; &gt; because those are also present inside strings in the code.

Is it possible to disable parsing code inside script tags?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

For parsing HTML, we use the mime type text/html

const htmlDoc = parser.parseFromString(data, "text/html");
const tags = htmlDoc.getElementsByTagName("script");
about 4 years ago · Santiago Trujillo Denunciar

0

Try encapsulating the JS code in a CDATA section so it looks like:

<script>
   <![CDATA[
      for (let i = 0; i < tags.length; i++) {....
  ]]>
</script>

These comments tell the xml parser to treat what is inside them as character data only

about 4 years ago · Santiago Trujillo 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