0

I would like to place a javascript (adsense) code inside the post (not above or after the post). It will be a HTML page.

Is there any way i can put my adsense code in external Js file and i will use one function to display it. adsense code looks something like

<script type="text/javascript"><!--
google_ad_client = "pub-xxxxxxxxxxxxxxxx";
google_ad_host = "pub-xxxxxxxxxxxxxxxx";
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

So if i call a function CallMe() which will start showing ad wherever i have used the function. In future if i would like to replace ad code with another code then i dont want to go to each post and replace it. I will just replace the adcode from js file.

I am a newbie and have just started learning JavaScript so i am really not aware if it can be done or not.

Any suggestion ?

3 Answers 3

1

Create file called AdSense.js with the following code:

google_ad_client = "pub-xxxxxxxxxxxxxxxx";
google_ad_host = "pub-xxxxxxxxxxxxxxxx";
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 336;
google_ad_height = 280;
function ApplyAdSense() {
    var oScript = document.createElement("script");
    oScript.type = "text/javascript";
    oScript.src = "http://pagead2.googlesyndication.com/pagead/show_ads.js";
    document.getElementsByTagName("head")[0].appendChild(oScript);
}

Now whenever you want adsense in your code, first include the file:

<script type="text/javascript" src="AdSense.js"></script>

Then call the function:

<script type="text/javascript">
    ApplyAdSense();
</script>

This way, until you call the function nothing happens.. and you can also comment the code inside the function to disable adsense throughout all your site.

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

6 Comments

Thanks a lot for answer..Let me try this first.. I will let you know if its working or not..
I have uploaded file on the sites.google.com and put <script type="text/javascript" src="www.sites.google.com/sites/xyz/adsense.js"></script> (not a real path)..and used code in the HTML file as follows: script type="text/javascript" src="www.sites.google.com/sites/xyz/adsense.js"></script><script type="text/javascript"> ApplyAdSense(); </script> Is it right ?
Can i use the complete code in <body> </tags> or i need to put something inside <head></head> ?
can i ask u something ? Have u used such method for your site? I tried but it only shows black space instead of ad..I think i should not play with adsense code because changing the adsense code is not allowed as per the adsense terms.. :-) Thanks anyways. I learned few things today.
They're probably using document.write so it's a dead end, sorry.. you'll have to put it in every page. However if you have server side language at your disposal it might make your life easier. Is there?
|
0

Wherever you want the ad to show up, place this code (assuming you have a function called CallMe).

    <some html>
        <script type="text/javascript">CallMe();</script>
    </some html>

1 Comment

Nope. callMe() was just an example... Do i need to put the code in the JS File?
0

If your concern is about the page loading time, Adsense released the asynchronous version of their Adsense code. Please see https://support.google.com/adsense/answer/3221666?hl=en

Comments

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.