Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed positioning an ad in IE

Can skip to the edit for a more up to date explanation.

I can't seem to set an ad that gets written out via document.write() to a fixed position. It works fine in all other browsers besides IE, and that includes IE9.

Here is an example: http://htinteractive.com/bottom_bar/demo.html

Any suggestions would be highly appreciated. I'm running out of ideas.

Thank you.

Edit:

I've narrowed the problem down to the following IE issue I'm having. To simplify it down...

<style type="text/css">
    #temp1
    {
        position:fixed; 
        bottom:0; 
        height:100px; 
        width:100px; 
        border:solid 2px red;
    }
</style>

<!--WORKS IN IE-->
<div id="temp1">
    <script type="text/javascript">
        document.write("<scr" + "ipt type=\"text/javascript\">\nif(typeof(cachebuster) == \"undefined\"){var cachebuster = Math.floor(Math.random()*10000000000)}\nif(typeof(dcopt) == \"undefined\"){var dcopt = \"dcopt=ist;\"} else {var dcopt = \"\"}\nif(typeof(tile) == \"undefined\"){var tile = 1} else {tile++}\ndocument.write('<scr'+'ipt src=\"http://ad.doubleclick.net/adj/shz.bloomington/home;pos=728x90_1;' + dcopt + ';tile=' + tile + ';sz=728x90;ord=' + cachebuster + '?\"></scr'+'ipt>');\n</scr" + "ipt>");
    </script>
</div>


<!--FAILS TO FIX POSITION IN IE-->
<script type="text/javascript">
    document.write('<div id="temp1">');
    document.write("<scr" + "ipt type=\"text/javascript\">\nif(typeof(cachebuster) == \"undefined\"){var cachebuster = Math.floor(Math.random()*10000000000)}\nif(typeof(dcopt) == \"undefined\"){var dcopt = \"dcopt=ist;\"} else {var dcopt = \"\"}\nif(typeof(tile) == \"undefined\"){var tile = 1} else {tile++}\ndocument.write('<scr'+'ipt src=\"http://ad.doubleclick.net/adj/shz.bloomington/home;pos=728x90_1;' + dcopt + ';tile=' + tile + ';sz=728x90;ord=' + cachebuster + '?\"></scr'+'ipt>');\n</scr" + "ipt>");
    document.write('</div>')
</script>

Anyways, I really need the 2nd method to work, and I'm pulling my hair out trying to figure out how.

Thanks.

like image 348
Serhiy Avatar asked Sep 26 '11 17:09

Serhiy


2 Answers

Maybe IE needs the temp1 DIV to be ended before writing the script element:

<script type="text/javascript">
    document.write('<div id="temp1"><' + '/div>');
    document.write("<scr" + "ipt type=\"text/javascript\">\nif(typeof(cachebuster) == \"undefined\"){var cachebuster = Math.floor(Math.random()*10000000000)}\nif(typeof(dcopt) == \"undefined\"){var dcopt = \"dcopt=ist;\"} else {var dcopt = \"\"}\nif(typeof(tile) == \"undefined\"){var tile = 1} else {tile++}\ndocument.write('<scr'+'ipt src=\"http://ad.doubleclick.net/adj/shz.bloomington/home;pos=728x90_1;' + dcopt + ';tile=' + tile + ';sz=728x90;ord=' + cachebuster + '?\"></scr'+'ipt>');\n</scr" + "ipt>");
</script>

EDIT (in response to your comment):

Since you want the ad script to go inside the div, you will have to put document.write aside and create the script programmatically, so you can simply call appendChild to put it into the div:

<script type="text/javascript">
    document.write('<div id="temp1"><' + '/div>');
    if(typeof(cachebuster) == "undefined") {
        var cachebuster = Math.floor(Math.random() * 10000000000);
    }
    if(typeof(dcopt) == "undefined") {
        var dcopt = "dcopt=ist;";
    } else {
        var dcopt = "";
    }
    if(typeof(tile) == "undefined") {
        var tile = 1;
    } else {
        tile++;
    }    
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://ad.doubleclick.net/adj/shz.bloomington/home;pos=728x90_1;"
        + dcopt 
        + ";tile=" + tile
        + ";sz=728x90;ord=" + cachebuster + "?";
    divTemp.appendChild(script);
</script>

Please note that I cannot be 100% sure I did not make some typo while reformatting the script, with all those escaped sequences...

like image 145
Luc125 Avatar answered Nov 09 '22 10:11

Luc125


Not tested, but seems like it should work:

<script>
(function(){
  var w = window,
    d = document,
    s = d.createElement('script'),
    div = d.createElement('div'),
    el = [].slice.call(d.getElementsByTagName('script'), -1),
    baseUrl = 'http://ad.doubleclick.net/adj/shz.bloomington/home;pos=728x90_1;';
  // end var block

  if(!("cachebuster" in w)){
    cachebuster = Math.floor(Math.random()*10000000000);
  }
  dcopt = "dcopt" in w ? "" : "dcopt=ist;";
  tile = "tile" in w ? tile+1 : 1;

  div.id = 'temp1';
  div.appendChild(s);
  el.parentNode.inserBefore(div, el);
  s.src = baseUrl + dcopt + ';tile=' + tile + ';sz=728x90;ord=' + cachebuster + '?';

})();
</script>
like image 1
Dennis Avatar answered Nov 09 '22 09:11

Dennis