A JS file referenced inside of another JS file is not executing correctly in IE

Hello Everyone,

My company sells ad space to various companies, some of which is through a third party. For various reasons, each advertisement is written using JavaScript. For example, if I need to write an ad to a particular div, it would be written the page as follows:

document.writeln("<div id=\\"someId\\">");
document.writeln("<a href=\\"somelink\\">");
document.writeln("<img src=\\"someimage.jpg\\" />");
document.writeln("</a>");
document.writeln("</div>");

I did not write this code, but for various reasons, this is how we need to write the ad to the page. However, for third party ads (which are referenced through yet another JavaScript file), it seems that the actual ad is being written to the page at a different time than the containing div, but only in IE. For example, the above code would become:

document.writeln("<div id=\\"someId\\">");
document.writeln("<script type=\\"text/javascript\\" src=\\"somescript.js\\">");
document.writeln("</script>");
document.writeln("</div>");

and would be output as

<div id="someId">
    <script type="text/javascript" src="somescript.js">
    </script>
</div>
.
.
.
<!-- code output by above js file: -->
<a href="someThirdPartyLink.html"><img src="someImage.jpg" /></a>

For some of our ads, this is causing some terrible design problems. As I mentioned above, I can’t change either of the JavaScript files, and I unfortunately can’t get the third party ad servers to place unique ID’s on their HTML elements.

Can you think of any way that I could get the output code from the second JS file to remain inside of the div created by the first JS file?

Thank you very much for your time and willingness to help.

-Matt

TriLLi,

Thanks for your quick reply. Unfortunately, I can’t do it like that since other ads also rely on the code we already have in place, and the HTML for each is different.

Basically, I’m just trying to figure out a way to force IE to place the element created by document.writeln(‘…’) within the element that contains the document.writeln JavaScript statement.

Hi,

you can do it like this

var div = document.createElement('div');
div.setAttribute('id', 'some_id');

var scr = document.createElement('script');
scr.setAttribute('type', 'text/javascript');
scr.setAttribute('src', 'exampl.js');
div.appendChild(scr);

document.getElementById('id_of_container').appendChild(div);

I hope this can help you.