Question

Is there a way, callback, hack to fire a script/JS function after Google has rendered its ads on your page?

I could simply use a timeOut function, but I'd rather not.

Was it helpful?

Solution

This is my idea (not tested):

Google creates a set of iframes in which it loads the ad's content. You can find them with a selector (they have an id that is something like: google_ads_frame_BLABLAH). Use the Browser DOM inspector to get the real ID. After finding them, you can add an "onload" listener to know when it's completely loaded.

An example in jquery / pseudocode:

$("iframe").each(function () {
    if ($(this).attr("id").match("/google_ads_frame.*/") !== null)
    {
        $(this).load(function () {
            alert("Ad is ready");
        });
    }
});

You can take count of ads' number that are loaded.

Watch out that if the iframe will internally redirect to another page, the load listener will fire twice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top