简体   繁体   中英

Why is this Javascript code for inserting an Adsense ad not working?

Starting with advice from this question , I did some searching and found some code on this web site , which is intended to insert a Google Adsense ad into a particular spot on my web page, after the page is loaded. In my case, I do some checking in Javascript, send JSON data back to my server, and then based on the response I get from that JSON, I determine whether or not to show the Google Adsense ad.

Since I got this code from the web, and didn't write it myself, there's a lot of it I don't get, and in some places I had to fill out details based on what I thought was required, but I was guessing a bit. This is how the code I'm currently using looks:

if(userStatus.status == 0)
{
    console.log("google ad should show");

    window["google_ad_client"] = 'ca-pub-0000000000000000';
    window["google_ad_slot"]  = "0000000000";
    window["google_ad_width"]  = 320;
    window["google_ad_height"]  = 50;

    window.adcontainer = document.getElementById('google-ad');
    window.adhtml = '';

    function mywrite(html)
    {
        adhtml += html;
        if(html == '</iframe>')
        {
            adcontainer.innerHTML = adhtml;
        }
    };

    document.write_ = document.write;
    document.write = mywrite;

    script = document.createElement('script');
    script.src='http://pagead2.googlesyndication.com/pagead/show_ads.js';
    script.type='text/javascript';
    document.body.appendChild(script);
}

It seems that it is almost working.

When I view my page in Firefox with Firebug enabled, I see that my console log message is being displayed, so I know I've got through all my conditionals.

I've also tried running the ad with the code written directly into the HTML, just to confirm that Google has accepted my web site and that they will display ads. When I do that, the ads display fine, so I believe there's nothing wrong with my Asense account.

However, while the call to http://pagead2.googlesyndication.com/pagead/show_ads.js is being appended to the end of the page, it seems that the ad parameters are not being written into the google-ad DIV as intended.

Thus, the ad is not displaying.

Where is my code going wrong?

Turns out the problem was the if(html == '</iframe>') line. I had thought it was something to do with how Google constructs the adsense ad, but it must be something particular to how the person who originally coded needed to construct their HTML.

In my case, though, I simply don't need it. My code now has been changed to:

function mywrite(html)
{
    adhtml += html;
    adcontainer.innerHTML = adhtml;
};

And much to my surprise, it works! I now have a Google Adsense Ad that will display depending on the results from JSON data that is retrieved after the page is loaded.

Also, further tip for anyone who might want to do something similar: First hard code your Google ad into the HTML to ensure that it works and that Google has accepted it before attempting to do anything fancy with Javascript or anything else. I probably could have saved myself some time if I had realized that earlier.

Hope that helps others who might want to do the same kind of thing.

I am not sure... Here are a few comments:

  1. I hope that you have div id google-ad box already created.

  2. document.write works only ONCE !
    when page is creating and DOM is forming. So that code must be at very top of js execution.
    write will not work if DOM/Page is already formed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM