Ads (again) - Need helping with javascript coding though

Technical Support
  • In this thread @TheBronx wrote about getting around the document.write issue: Re: NodeBB and ads - A never ending story

    My site wasn't approved for AdSense (or DFP) due to a lack of volume. I am working on building site activity but in the meantime I have signed up with Chitika for publishing ads. Chitika uses document.write and I would really like to get their code bit working with the little trick that TheBronx devised.

    Would someone be willing to help me re-write this code block to work as he outlined?

    This is what he posted:

    Here is some code:

    var originalFunction = document.write;
    
    overrideDocumentWriteFunction = function(container) {
    	document.write = function(content) {
    		container.innerHTML = content;
    		document.write = originalFunction;
    	};
    };
    

    And I used it like this:

    var div = document.getElementById('adPlacement');
    overrideDocumentWriteFunction(div);
    
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.innerHTML = '{adsense script content}'
    document.body.appendChild(script);
    

    This way the document.write function was customized to ensure the Adsense script worked after the page was loaded.

    This is the Chitika ad code:

    <script type="text/javascript">
      ( function() {
        if (window.CHITIKA === undefined) { window.CHITIKA = { 'units' : [] }; };
        var unit = {"calltype":"async[2]","publisher":"myPublisherId","width":728,"height":90,"sid":"Chitika Default"};
        var placement_id = window.CHITIKA.units.length;
        window.CHITIKA.units.push(unit);
        document.write('<div id="chitikaAdBlock-' + placement_id + '"></div>');
    }());
    </script>
    <script type="text/javascript" src="//cdn.chitika.net/getads.js" async></script>
    

    If anyone would like to lend a hand in re-packaging their code with the proposal that TheBronx made I would be very grateful and appreciative.

    Thank you.

  • @rod you should just be able to do something this:

    First, this script should go in your footer so it won't run before your ad insertion scripts:

    <script type="text/javascript" src="//cdn.chitika.net/getads.js" async></script>
    

    Next, put the inline script where you want the ad to show up. Notice that you need to change two lines. Add the <div... line before the script, and change the document.write... line to the document.getElement... line shown below.

    <div id="chitika-ad"></div>
    <script type="text/javascript">
      ( function() {
        if (window.CHITIKA === undefined) { window.CHITIKA = { 'units' : [] }; };
        var unit = {"calltype":"async[2]","publisher":"myPublisherId","width":728,"height":90,"sid":"Chitika Default"};
        var placement_id = window.CHITIKA.units.length;
        window.CHITIKA.units.push(unit);
        document.getElementById('chitika-ad').id = 'chitikaAdBlock-' + placement_id;
    }());
    </script>
    
  • @PitaJ If I could upvote this 100 times I would. Thank you kindly.

  • Followup: It seems to be working fine. Ad refreshes as I navigate around the forum. No forced page reload necessary.


Suggested Topics