Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Can you help with inserting this Adsense Code?

    I'm trying to insert an Adsense block with jQuery after .myclass on my website but so far no success...
    Does anyone know how I can make this work?
    Any help would be much appreciated...

    This is what I've tried so far, but it's not working:

    Code:
    $('
      <script type="text/javascript"><!--
      google_ad_client = "ca-pub-xxx";
      google_ad_slot = "xxx";
      google_ad_width = xxx;
      google_ad_height = xxx;
      //--></script>
      <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
    ').insertAfter('.myclass');
    Last edited by jeanpaul1979; 01-18-2013 at 02:33 PM.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You have to insert the AdSense code exactly as supplied by Google in the place you want the ad to appear because their code uses antiquated write statements designed for Netscape 4 and earlier browsers which is incompatible with modern JavaScript.

    Because of the antiquated code used by Google their ad code will always be inserted after the script that inserts it into the page and not in the spot in the page where you tell it - even where you manage to get your script to insert their script into the desired spot in the page before the page finishes loading - because the write statements will add after the current script regardless of where they are run from.
    Last edited by felgall; 01-18-2013 at 09:21 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Finally found a solution that actually works.

    First you get the postscribe.js and htmlparser.js files from https://github.com/krux/postscribe

    Next you create a separate script file for each ad:

    Code:
    google_ad_client = "pub-9999999999999999";
    google_ad_slot = "9999999999";
    google_ad_width = 300;
    google_ad_height = 250;
    postscribe('#adv1','<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"><\/script>');
    Now you can place a div tag where you want the ad to appear:

    Code:
    <div id="adv1"></div>
    and the following script tags just before the </body> tag:

    Code:
    <script type="text/javascript" src="inc/postscribe.js"></script>
    <script type="text/javascript" src="inc/htmlparser.js"></script>
    <script type="text/javascript" src="inc/ad1.js"></script>
    If you want more ads then just create an ad2.js file for it similar to the first one, add a div where that ad is to appear and add one extra script tag to the bottom of the page. Repeat for additional ads (the postscribe call must be the last line in the script or it doesn't insert the ad properly before reading further parameters and jumbling the ad info).

    Thankyou for your original post about this - I had looked for something to do this ages ago and hadn't been able to find anything. Your post reminded me to look again and this time I was successful - its a lot of code just because Google hasn't updated their code to get rid of the long obsolete document.write statements though.
    Last edited by felgall; 01-19-2013 at 08:57 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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