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

237
Views
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 answers
Answer question

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 Report

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 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!