SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Location
    Wheaton, IL
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    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:

    Code:
    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

    Code:
    <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

  2. #2
    SitePoint Enthusiast TriLLi's Avatar
    Join Date
    Feb 2010
    Location
    BiH, Bugojno
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    you can do it like this

    Code JavaScript:
    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.
    http://www.wiseblog.info
    Programmers don't die, they just GO SUB without return.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Location
    Wheaton, IL
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •