Forum Moderators: martinibuster

Message Too Old, No Replies

Placing Ads in JavaScript with a Conditional IF Statement

         

peterinwa

6:26 am on Mar 28, 2006 (gmt 0)

10+ Year Member



Google AdSense code is designed to drop into a page of HTML. What I am asking is if you can drop it into JavaScript code conditionally.

My purpose is to be able to place ads only on pages which meet certain conditions. I know how to test for this with JavaScript; I don't know if I can include the AdSence code within the JavaScript code.

I could post about this in the Webmaster World JavaScript forum but I am afraid to display my AdSence code to ask for help. So I'm hoping that someone here is familiar with JavaScript.

Thanks, Peter

jomaxx

6:43 am on Mar 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you can do this conditionally with PHP or SSI or another form of server-side scripting. I don't believe there's any way to do so using Javascript alone without changing the code, which as you probably know is prohibited.

peterinwa

7:06 am on Mar 28, 2006 (gmt 0)

10+ Year Member



That pretty much confirms what I thought. I only know HTML and JS, but thanks. (I do everything on the client side in their browser.)

Peter

swa66

11:40 am on Mar 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When in doubt, setup a demo page on a hidden URL and ask google for a clean bill of health before you put it all over your site.

Even just the fact that you asked will earn you credit should they not like it in the long run.

Bluepixel

2:41 pm on Mar 30, 2006 (gmt 0)

10+ Year Member



But on the other site, they can disallow it, and you would be better off just doing it.

milanmk

3:15 pm on Mar 30, 2006 (gmt 0)

10+ Year Member



<script language="javascript">

if (condition = true)
{
AdSense code goes here
}

function blockError(){ return true; }
window.onerror = blockError;

</script>
<script type="text/javascript" src="js from googlesyndication"></script>

You have to add that blockError function because without your AdSense code (in the case when you are not showing the ad on specific page) it will generate an error for google variables.

Make sure you ask the authorities before implementing this code.