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

174
Views
Insert special script to body with javascript

I know how to insert standard script to body with javascript.

Actually I'm using this:

document.body.appendChild(document.createElement('script')).src = 'https://url.com';

But I have found a script that looks different and i'm not very sure how I shall proceed:

<script type="text/javascript"> var _iub = _iub || []; _iub.csConfiguration = {"countryDetection":true,"reloadOnConsent":true,"consentOnContinuedBrowsing":false,"perPurposeConsent":true,"purposes":"1,2,3,4,5","lang":"en","siteId":xxxx,"cookiePolicyId":494xxxx,"cookiePolicyUrl":"https://www.iubenda.com/privacy-policy/xxxx", "banner":{ "acceptButtonDisplay":true,"customizeButtonDisplay":true,"rejectButtonDisplay":true,"position":"float-bottom-center","backgroundOverlay":true }}; </script> <script type="text/javascript" src="//cdn.iubenda.com/cs/iubenda_cs.js" charset="UTF-8" async></script>

How I can insert this script into the body using javascript? As it contains vars

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

0

This is how google use to append scripts dynamically.

Please Note the async property marked as true so you won't need to worry about the sequence of the resource loading (dom parsing that might halt the DOM tree creation)

When async is marked as true, the script will be downloaded and executed as soon as possible (after the script has been downloaded) and HTML page will be parsing simultaneously.

When async is marked as false, the process of script downloading and execution will be carried out before starting of any HTML page parsing hence HTML parsing will halt while script is downloaded (which is not good for performence)

(function () {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.async = true;
    script.src = 'https://url.com';
    var element = document.getElementsByTagName('script')[0];
    element.parentNode.insertBefore(script, element);
})();

You can create a TextNode element and append it to the script to match your case. For example:

 var inlineCode = document.createTextNode('alert("hello world")');
 script.appendChild(inlineCode); 
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!