0

I have a js function that should get some advertisement js code from some server and place it in specified DOM element. It looks like this:

function LoadAd(scriptContainer)
{           
    var js = document.createElement("script");
    js.async = true;
    js.src ='someAdUrl';
    var sHolder = document.getElementById(scriptContainer);
    if (sHolder != null) {
        sHolder.appendChild(js);
    }
}

the argument 'scriptContainer' is an ID of DOM element, that should contain created js element.

This external js file contains a lot of code that should provide an ad. But this code is never reached and never executed.

When I put this js src directly in html:

<script src='someAdUrl'></script>

it works fine. I've checked, the js file is being loaded.

Any ideas?

EDIT: Here is an example of content of js file:

document.write('<!-- Some Message -->\n\n<a href="SomeUrl" target=\"_blank\">\n<img width=\"336\" height=\"110\" border=\"0\" src="someImageSource">\n</a>\n');

And it always contains document.write

Gev
  • 429
  • 6
  • 13

2 Answers2

1

If the external JS file is being loaded, have you checked whether or not the function is actually being invoked and run?

If the function isn't self-executing and you don't explicitly call it, the code won't run.

James
  • 10,716
  • 1
  • 15
  • 19
1

I use this code in my case. It may help you

var js=document.createElement('script');
js.setAttribute('src','http://domain.com/js/jscpt.js');
document.body.appendChild(js);
vusan
  • 4,634
  • 4
  • 36
  • 76