5

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.

b.stevens.photo
  • 876
  • 2
  • 9
  • 18

2 Answers2

6

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();
    });
supra28
  • 1,646
  • 10
  • 17
3

Try this function:

(function () {
    if (typeof window.R === 'undefined') {
        var s = document.createElement('script');
        s.setAttribute('src', 'source goes here!');
        document.body.appendChild(s);  // this appends the script to the body you can also use document.head.appendChild(s) to append to the head
    }
}());
Mandalina
  • 446
  • 5
  • 22