1

This question probably has an easy answer, yet it evades me. In the ads on my site (Adsense or otherwise), they provide a script tag (e.g. <script type='text/javascript' src='to/my/source'></script>) to place in the location I want the ad to appear.

However, before I load the ad script, I want to test to see if the user is on a handheld device by testing the CSS display of a div in js. Like this:

if ($('#mydiv').css('display') == 'none') {
    //do not load the ad; do nothing
} else {
    //load the ad script element at *this place*;
    //that is, where this conditional is placed
}

My thought is to place this conditional in the same place as I would place the ad script normally. However, it's not working (just like I knew it wouldn't), and I can't figure out how to load the script tag in place. Thanks for the help.

EDIT:
Here is my actual code:

<script type='text/javascript'>
if ($('#sidebar-wrapper').css('display') == 'none') {

} else {
    document.write("<scr" + "ipt type='text/javascript' src='http://ads.blogherads.com/bh/32/320/310566/1343256/300a.js'>" + "</scr" + "ipt>");
}
</script>
preahkumpii
  • 1,301
  • 4
  • 21
  • 36
  • 1
    Are you using php or asp pages? Typically this would need to be a conditional on the server side of things. – caspian Jun 13 '14 at 02:31

1 Answers1

1

The only way I know of to conditionally insert a script directly into a spot in the HTML is to use document.write().

<script>
if ($('#mydiv').css('display') == 'none') {
    //do not load the ad; do nothing
} else {
    document.write("<scr" + "ipt src='http://example.com/ad.js'></scr" + "ipt>");
}
</script>

Note: you will have to make sure that jQuery is already loaded and that #mydiv appears before this location in the HTML of the page for your conditional to work properly.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Ok, but since the code above is found within a script tag, adding another script tag within it is causing a problem. Its just loading this: `'); } `. – preahkumpii Jun 13 '14 at 03:33
  • 1
    @preahkumpii - This technique works just fine. `document.write()` inserts content into the DOM at the current DOM parse position (which is NOT in the middle of the script) - it's right after the script. If you're not getting the right content, then perhaps something is not quite right with your code (which we can't help you with unless we can see the actual code). – jfriend00 Jun 13 '14 at 04:33
  • @preahkumpii - there doesn't seem to be anything at `http://ads.blogherads.com/bh/32/320/310566/1343256/300a.js` so I can't examine that script. Chrome says: "The resource could not be found.". – jfriend00 Jun 13 '14 at 04:55
  • Thanks. The exact same code is still not working for me. I'm going to keep checking. If you could delete the html file above, I would appreciate it. I don't want the ad server to come calling for serving to a different domain. – preahkumpii Jun 13 '14 at 11:41
  • 1
    @preahkumpii - OK, file and link removed. Can you post a link to the page you're trying to use it in so I can see exactly what you're doing and perhaps do some debugging on it. The concept works fine if implemented correctly. – jfriend00 Jun 13 '14 at 15:48
  • [pintsizedtreasures.com](http://pintsizedtreasures.com). The ad is the first ad in the sidebar—BlogHer. BTW, I found the problem. Since I am in Wordpress, I needed to use `jQuery` instead of `$` in the jQuery functions. Duh. I always forget that. – preahkumpii Jun 14 '14 at 08:09
  • I'm curious. Why did u divide up the string inside the `document.write()` function? I noticed that is only works if you divide it up. – preahkumpii Jun 14 '14 at 10:09
  • 1
    @preahkumpii - see http://stackoverflow.com/questions/236073/why-split-the-script-tag-when-writing-it-with-document-write. I assume this would not be an issue if the JS was in an external JS file (because that file is not processed for `` tags by the HTML parser). – jfriend00 Jun 14 '14 at 17:06