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

146
Views
add external html with javascript code without using iframe

I'm trying to load some html and javascript from one website without using iframe. I can render the html, css and it seems that the javascript files are being loaded, but I can't call functions from the loaded content.

I'm trying to load using fetch:

window.onload = function () {
    fetch(myURL)
        .then(function (response) {
            return response.text();
        })
        .then(function (body) {
            var dv = document.createElement('div');
            dv.innerHTML = body;
            document.body.appendChild(dv);
            //trying to call function here after appending the html content to my page
        });
}();

Is it possible, or it's blocked by browsers for security reasons?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you have to explicitly add a script element.
so what works for sure is:

 let text = "<h1>Test</h1><script>const callMe = () => { alert('called'); }" + "&lt;/script>";
    const scriptStart = text.indexOf('<script>');
    const scriptEnd = text.indexOf('&lt;/script>');
    const script = text.substring(scriptStart + "<script>".length, scriptEnd);
    text = text.substring(0, scriptStart) + text.substring(scriptEnd + "&lt;/script>".length);
    element.innerHTML = text;
    const scriptElement = document.createElement("script");
    scriptElement.innerHTML = script;
    document.head.appendChild(scriptElement);
    callMe();
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!