2

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

k3nshx
  • 31
  • 3
  • Does this answer your question? [How to append in JavaScript?](https://stackoverflow.com/questions/9413737/how-to-append-script-script-in-javascript) – Pietro Nadalini Sep 21 '21 at 17:24
  • What do you mean by "I have found a script that looks different and i'm not very sure how I shall proceed" what do you expect from your code, and what is your current output. – Pietro Nadalini Sep 21 '21 at 17:25
  • @PietroNadalini I though it was clear the question, but updated with my question which is how I can insert that script that contains var to the body using javascript. – k3nshx Sep 21 '21 at 17:27
  • What's the problem with the vars? How does it make it any different? – Rojo Sep 21 '21 at 17:34
  • Hi @Rojo, I'm not sure where to place the "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":"iubenda.com/privacy-policy/xxxx", "banner":{ "acceptButtonDisplay":true,"customizeButtonDisplay":true,"rejectButtonDisplay":true,"position":"float-bottom-center","backgroundOverlay":true }};" code. – k3nshx Sep 21 '21 at 17:42
  • Does this answer your question? [Adding text to an existing text element in JavaScript via DOM](https://stackoverflow.com/questions/41764061/adding-text-to-an-existing-text-element-in-javascript-via-dom) – Rojo Sep 21 '21 at 18:04

1 Answers1

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); 
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
  • And where should be added the var that contains the second script snippet ? – k3nshx Sep 21 '21 at 17:29
  • I modified the script – Ran Turner Sep 21 '21 at 17:30
  • Hello @Ran Turner, but not sure if I'm not understanding the script but I don't see where the "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 }};" should be added? – k3nshx Sep 21 '21 at 17:39
  • Look at the bottom of my answer my friend. It will help you – Ran Turner Sep 21 '21 at 17:44