Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

50
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

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

7 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.