Jump to content

AJAX for Adsens


possien

Recommended Posts

I am developing a website that can be viewed in smart phones to PC's.  CSS is used to determine screen size.  Adsense won't let you use display:none for their ads because they still show in the html. 

 

I wrote this AJAX call display to the appropriate ad size for the screen size but it doesn't work

 

The js file is:

function getWidth(int)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("width").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","includes/width.php?width="+int,true);
xmlhttp.send();
}

 

the server php file has:

if(isset($_GET['width'])){
$width = $_GET['width'];
if($width <= 800){
//echo code for banner 320x50

echo '<script type="text/javascript"><!--
google_ad_client = "xxx";
/* cb_sm_banner */
google_ad_slot = "5498561767";
google_ad_width = 320;
google_ad_height = 50;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>';
}
else if($width > 800 && $width <= 959){
//echo code for banner  468x60
echo '<script type="text/javascript"><!--
google_ad_client = "xxx";
/* cb_med_banner */
google_ad_slot = "4876498027";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>';
}
else if($width >= 960){
//echo code for banner 728x90
echo '<script type="text/javascript"><!--
google_ad_client = "ca-pub-xxx";
/* cb_large_banner */
google_ad_slot = "2036334501";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>';
}
}
else $width = "";

In the page where I want it to display I have:

<div align="center" id="width">
    <script type="text/javascript">
     getWidth(window.innerWidth)</script>
  </div>

I can put html code in the PHP file and it echos it just fine but not javascript, any ideas?

Link to comment
https://forums.phpfreaks.com/topic/257725-ajax-for-adsens/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.