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

180
Views
jquery append and run tag <script> VS js vanilla

I am updating my application and I have decided to use bootstrap 5 which no longer has the jquery dependency and to eliminate this dependence from my application as well.

So I'm rewriting the javscript part from jquery to js vanilla.

I use AJAX a lot to load content in different divs and for their subsequent manipulation based on certain events triggered by the user. Unfortunately, in some cases I find myself forced to hang a script tag together with the html part to run a javascript function that I cannot execute in any other way.

For example:

<script>myFuncrion(val1, val2, val3);</script>
<div id="myDiv">
  <div>Other html code ..</div>
</div>

With jquery something like this was enough to append the markup to a given div and execute the javascipt function

$('#elementToAppend').append(html);

Converting this to vanilla js I get something like this

document.getElementById('elementToAppend').innerHTML = html;

Everything is correctly appended, but obviously the js is not executed

I don't know jquery behind the scenes, but I assume it is being manipulated in some way for the javascript part to run.

I then created a function that would get everything back to working

const functionOne = (elem) => {
  // I make the AJAX call...
  let jsAndData = getJsAndData(data);
  
  // Append the html string
  document.querySelector(elem).innerHTML = jsAndData.cleanData;
  // I run the js
  eval(jsAndData.js);
}

const getJsAndData = data => {
  var js = "";

  // Check if there is a <script> tag
  let script = data.match(/<script>(.*?)<\/script>/g);

  // If there is a script tag I extrapolate the js content
  if (script) {
    script.map(val => {
      js = val.replace(/<\/?script>/g, "");
    });

    // I remove the <script> tag and its content from the html string
    let cleanData = data.replace(/<script[^>]*>.*<\/script>/gm, "");

    return { cleanData: cleanData, js: js };
  } else {
    return { cleanData: data, js: js };
  }
};

In this way I get the same result I had with jquery, even better, because on the DOM there is no longer any trace of the script tag

Now, my questions are:

  1. Am I doing things right?
  2. Is there a better way to do this?
  3. Am I completely out of the way?
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your way is a correct way. For security reasons (XSS) the script tag is not run when using innerHTML.

https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML#security_considerations

Alternative: In MDN you can find hacky code like this to work around from the backend without touching the frontend. I would not recommend this way.

const name = "<img src='x' onerror='alert(1)'>";
el.innerHTML = name; // shows the alert
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!