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

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

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