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

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

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