0
<html>
    <head> 
       <script async src="https://www.googletagmanager.com/gtag/js?id=xxxxx">         
       </script>
<html>
        replace value xxx by var get from config 
<script async  src="https://www.googletagmanager.com/gtag/js?id=Var">

I want to replace the value of the id in src of script with a variable that I get from conf file

any help?

radis
  • 1
  • 2
  • once the page loaded, get the src of the whatever script you needed, manipulate it and create a new script tag with the id you get after manipulation and append to head.try this – Aravind S Jun 27 '18 at 14:15
  • 1
    think this might be helpfull: https://stackoverflow.com/questions/8578617/inject-a-script-tag-with-remote-src-and-wait-for-it-to-execute – mrdeadsven Jun 27 '18 at 14:20
  • Add the script tag to your document `` and give it an id, e.g.: `` and once that is done simply call the Specific tag with its **id** `const script = document.getElementById('async_script');`, then simply just replace it like this `script.src = script.src.replace('xxxx', number);`. – Bargros Jun 27 '18 at 14:53

1 Answers1

0

As HTML is "read" by the web browser, you won't be able to simply add a variable to a <script> tag. You can accomplish this through pure Javascript by loading in the file dynamically as stated here: Dynamically load JS inside JS

Where id_number is the number you are attempting to add:

var url_string = 'https://www.googletagmanager.com/gtag/js?id=' + id_number
var script = document.createElement('script');
script.onload = function () {
    //do stuff with the script
};
script.src = url_string;

document.head.appendChild(script);
Jake Chasan
  • 5,528
  • 5
  • 37
  • 81