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

125
Vistas
Import and consume Javascript inside shadow DOM

I've created a shadow dom in javascript and everything is working as intended. Except, I want to add a remote JS script to it and consume a function it contains. But don't know how to do this.

This is an piece of code I use for the creation of my shadow DOM:

var shadowDom = this.attachShadow({ mode: 'open' });
var html = "<div class='stuff'>Hi!</div>";

let wrapper = document.createElement('div');
wrapper.innerHTML = html;

const js = document.createElement('script');
js.setAttribute('src', 'http://mywebsite.com/js/code.js');

shadowDom.appendChild(js);
shadowDom.appendChild(wrapper);

The code.js script includes:

function test()
{
    alert("ok");
}

How can I call the test() function once I've added the code.js to my shadow DOM inside my widget's script?

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The easiest way to auto call a function is to use the following syntax in your imported code.

function test()
{
    alert("ok");
}()

This should execute the function as soon as the code is evaluated.

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