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

149
Views
How can I add a minified js script to the dom and execute it?

My question is very similar to this one. The difference is that I am trying to add a minified js code in the script tag. If I type some simple code, it works. For example, run this on a jsfiddle:

var s = document.createElement( 'script' );
var code = `
        console.log("success");
        `;
    
    try {
      s.appendChild(document.createTextNode(code));
      document.head.appendChild(s);
    } catch (e) {
      s.text = code;
      document.head.appendChild(s);
    }

However, try replacing the console.log("success"); with the contents from this link.

You will get the error "Uncaught SyntaxError: Invalid or unexpected token". Which is weird because...if I add the script using the source attribute and url of the file, it works. I don't understand why adding the contents directly fails.

What am I doing wrong, and how can I make it work? Thank you for your time.

Here is the jsfiddle with the non-working code

about 4 years ago · Juan Pablo Isaza
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!