Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically insert script tag? [duplicate]

So i have this script tag

 <script async="async" data-cfasync="false" src=""></script>

That I'd like to be able to insert dynamically. I've tried setting it to a variable,

 let scriptTag = '<script async="async" data-cfasync="false" src=""></script>';

and adding it using innerHTML to my content div but that doesn't seem to work. I know I'm going about this very wrong and part of me thinks this probably isn't even possible.

The reason I want to do this is so that I can turn ads on and off for testing purposes so my views during testing don't affect the analytics, is this something I should even be worrying about or is it negligible. I just know that I've been banned from chartboost for exactly this without having a testing variable that turned ads on and off.

Edit: Yes this is similar to other questions but those don't address using attributes like 'data-cfasync'. Mandalina and supra28 nailed it on the head.

like image 907
b.stevens.photo Avatar asked Sep 07 '18 18:09

b.stevens.photo


1 Answers

It seems you are not adding your script tag correctly. Here is one way you can do it:

 const script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "";
    script.async = true;
    script.dataset.cfasync = false;  
    document.body.appendChild(script);
    script.addEventListener("load", () => {
      console.log("Script added successfully");
      resolve();
    });
like image 78
supra28 Avatar answered Oct 15 '22 12:10

supra28