Sign in with
Sign up | Sign in
Your question

Javscript help

Last response: in Applications
Share
February 9, 2011 7:25:25 PM

Hey,

If anybody is knowledgeable, I really could use some help here.

I'm trying to write a script that makes it so that when using a screen width of 1280 or lower, Google shows one ad at the top of the page. With screen widths over 1280, it shows 2 ads along the side of the screen.

I've checked it several times and I don't know what I've done wrong (but I'm also a beginner). The script works flawlessly in all versions of Firefox for both high and low res (I checked with FF 2.0, 3.0, 3.5, and 3.6) but in IE6 and IE8 while displaying almost correctly at the high resolution, it gets completely screwed up at the low resolution.

Basically I'm just using Javascript to insert HTML comment tags around the ads that I don't want to appear for that resolution.








<!-- Test run of Google AdSense -->
<!-- One ad floats left, one floats right, the rest of the body is center aligned in between (high res only) -->
<!-- There are 3 ads, scripting makes it so that 2 appear on the sides with a screen width above 1280 pixels, and below 1280 pixels just one appears across the top -->


<script type="text/javascript">
<!--
var skip1 = "<!--";
if ( screen.width <= 1280 )
{
document.write (skip1);
}
//-->
</script>

<div style=" float:left; font-size:7pt; color:#777; ">
Advertisement<br />
<script type="text/javascript"><!--
google_ad_client = "pub-9736870014804453";
/* 160x600, created 2/2/11, left side homepage */
google_ad_slot = "6021069285";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div style=" float:right; font-size:7pt; color:#777; ">
Advertisement<br />
<script type="text/javascript"><!--
google_ad_client = "pub-9736870014804453";
/* 160x600, created 2/2/11, homepage */
google_ad_slot = "3502793671";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<script type="text/javascript">
<!--
var skip2 = "-->";
if ( screen.width <= 1280 )
{
document.write (skip2);
}
//-->
</script>


<script type="text/javascript">
<!--
var skip3 = "<!--";
if ( screen.width > 1280 )
{
document.write (skip3);
}
//-->
</script>

<div style="font-size:7pt; color:#777;">
<center>
Advertisement<br /><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9736870014804453";
/* low res top ad */
google_ad_slot = "4610054252";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</center>
</div>

<script type="text/javascript">
<!--
var skip4 = "-->";
if ( screen.width > 1280 )
{
document.write (skip4);
}
//-->
</script>


<!-- End of Adsense side banners -->

Apparently it doesn't work in Opera, Chrome, or Safari either and the code doesn't validate as XHTML so there is definitely something wrong. :( 

More about : javscript

a b L Programming
April 18, 2011 12:30:42 AM

They way I would do it is to add two empty div tags to the page where the ads go, each with a unique id. Then use JavaScript code to modify the HTML inside the div that is to be used.

For many examples search on: JavaScript modify div content
m
0
l
!