0

I am trying to incorporate my own advertisement module for my website, if their are no ads to be displayed it defaults to google adesense ad

I currently have

if(this.ad_found)
//run code to display ad
else {
    google_ad_client = "ca-pub-";
    /* Name */
    google_ad_slot = "-----";
    google_ad_width = ---;
    google_ad_height = ---;

    var head = document.getElementById('id_from_display_page')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = '//pagead2.googlesyndication.com/pagead/show_ads.js';
    $("#id_from_display_page").html(script);
}

as you can tell I have no idea how to populate id 'id_from_display_page' to display the google adsense stuff

The display page:

<div id="id_from_display_page"></div>

I want to populate this div with google adsense

1 Answer 1

0

EDITS:

This time, we're going to wrap all of this in a self-executing function. Also, we'll make new elements and stick em in the body at the end. Here's the cheat sheet:

Javascript:

(function (window, document) {
    if (ad_found) {
        //run code to display ad
    } else {
        google_ad_client = "ca-pub-";
        /* Name */
        google_ad_slot = "-----";
        google_ad_width = "---";
        google_ad_height = "---";

        var head = document.createElement('div');
        head.id = "id_from_display_page";
        var script = '<script src="//pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"><\/script>';
        head.innerHTML = script;
        document.body.appendChild(head);
    }
})(this, this.document, undefined);

Unfortunately without those variables set I have no way of actually testing this. But I decided to place the <script> inside of a new <div> element that would have to get parsed by the DOM and should result in the code being executed as expected.

Keep me posted.

Besides the missing variables, everything seems smooth with the fiddle I made below :)

Demooooooooo

Sign up to request clarification or add additional context in comments.

3 Comments

EDIT: It is running the script, I tested with another js file that alerts hi, and it did. The google adsense stuff isn't showing though
hahahaha well to be ABSOLUTELY SURE you've got a backup set of code I just re-did for you. Either way you go, you can't go wrong really.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.