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

113
Views
Alternate methods for injecting js code into index.html in react

I am working on a project where I have to inject js code into index.html in a react app. But I have to do this without actually touching the index.html file itself.

I have tried using the script tag in 2 ways:

 let script = document.createElement("script");
 script.type = "text/javascript";
 script.async = true;
 script.src = src;
 document.head.appendChild(script);

In approach 1: I added an external js file instead of the src. Error: Uncaught SyntaxError: Unexpected token'<'

In approach 2: I used script.innerHTML and added the entire code. Error: Uncaught SyntaxError: Invalid or unexpected token

Note1: Both errors are coming in the console of the browser

Note2: If I directly add the js code into index.html, everything seems to work

Note3: I have not included the js code because it is of an external library and might deviate the focus from the question, and it might even violate the guidelines for minimum code examples

Neither of the approaches seem to work for me and I have been trying to fix the errors, but I haven't been able to do so. Hence I wanted to ask if there are any other methods to do this as I am unable to find relevant solutions for the same.

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

0

Your code works fine in my browser without any errors. Other than adding the script tag, is there any other error? I think there is another error. Take a look at the code again.

// Added script tag to index.html.
// I created an empty file called app.js in the same location as index.html.

const tag = document.createElement('script');
tag.type = "text/javascript";
tag.async = true;
tag.src = './app.js';
document.head.appendChild(tag);
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!