2

We use an outside system to serve ads on our site. Currently in our header files, we have some js which uses jquery to insert the data to our ad holder which is a div that appears on every page, i.e.

$("#adSpot").prepend('put my ad here');

Our third party ad system just started using Google Ad Server another system to serve ads so now have given us some JS to call. I'd like to use our header files and not have to touch every file but I'm not having luck inserting js that is then executed so:

$("#adSpot").prepend('GA_googleFillSlotWithSize("ca-pub-981", "Page_1", 468, 60)');

Basically, I'd like to use the header file so when the page is loaded, it injects this js code into the div where we want the image placed and then the js is executed so the image will appear in the spot. Now it does push the code to the div but the js isn't executed.

Thanks!

  • 1
    Is it just me or 50% of the jquery questions on SO have for answer "$(document).ready" ? :) – marcgg Aug 24 '09 at 23:36

4 Answers4

3

In your header.php, replace the $("#adSpot).prepend() line with this code:

var adCode=GA_googleFillSlotWithSize("ca-pub-981", "Page_1", 468, 60);
$("#adSpot").prepend(adCode);

What do you get when you try this code?

Ali
  • 261,656
  • 265
  • 575
  • 769
  • The reason we used the prepend is that is the div where we want the ad to be displayed. The js code that gets executed simply goes out and grabs the ad so we need to somehow get the js in the div where we want the image and get it executed so it will grab the image. I can go through and add it to the adspot div on every page but was trying avoid that. The prepend currently allows me to change it in the header but can't get the js to execute once put into the div. –  Aug 24 '09 at 23:34
  • 1
    I've updated my answer, see if that helps you. If you can edit your question and add the code you are given by your new provider that will also help understand your question better. – Ali Aug 24 '09 at 23:37
  • I just updated. I've tried several things and believe I have tried what you suggested but can try again. Please see my update to see if it is any clearer what I need to do. Thanks. –  Aug 24 '09 at 23:51
  • Have a look at my updated code. What do you get when you try what i've given this time? If you're using fireftp and firebug can you tell us what, if any error message you get? – Ali Aug 24 '09 at 23:56
  • The code does appear to execute but seems like it is happening twice and it doesn't put it in the adSpot, it throws it at the top right under the body tag and when I browse in firebug, it doesn't show anything in the adspot div. I also changed the code to an alert to test and noticed the alert fired twice: –  Aug 25 '09 at 00:22
  • I should clarify, if I don't wrap the code in $document ready then it doesn't prepend but simply throws the add at the top. If I do wrap it, the ad and page flash on the screen and then it tries to load another page as the screen goes blank. –  Aug 25 '09 at 01:33
1

Don't really understand why you are doing it this way, but seeing as you insist on putting the JavaScript in the div, have you tried wrapping it in a script tag and prepending it to the div?

$("#adSpot").prepend('<script type="text/javascript">GA_googleFillSlotWithSize("ca-pub-981", "Page_1", 468, 60)</script>');
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
micmcg
  • 2,372
  • 18
  • 16
  • Basically we have a very large site and need to change how we serve ads. Currently we have a header file which uses JS to inject the ad data into a div. Our ad provider has changed and instead of giving us a static url to call, they now provide us with JS. I'd like to utilize the same js functions as to not touch every file but when we inject the javascript code into the div, it doesn't seem to fire. –  Aug 25 '09 at 00:46
  • The code you posted throws an error of unterminated string literal and marks the first single quote –  Aug 25 '09 at 00:50
  • I just copied that line, pasted it into the firebug console and ran it. No error. Check the rest of your code. – micmcg Aug 25 '09 at 08:32
  • regarding prepending script tags http://stackoverflow.com/questions/610995/jquery-cant-append-script-element – Timo Huovinen Nov 11 '11 at 11:34
0

Since it seems you are using jquery already:

$(document).ready(function() {
    //dom is ready
});

// alternatively:
$(function() { 
    // dom is ready
});

Both are the same, the second is just a shorter notation for the first.

TM.
  • 108,298
  • 33
  • 122
  • 127
  • Yes, that code is wrapped in $(document).ready but the problem is it inserts the javacript into the adSpot div but I need it to insert the javascript and then have something execute the javascript. The insert works fine but the js that is put into the div doesn't fire. –  Aug 24 '09 at 23:26
0
$(document).ready(function(){
  // Your code here...
});

Source

Randolpho
  • 55,384
  • 17
  • 145
  • 179