29
mai

jQuery et google adSense

jquery_and_adsense
Voici la méthode qui va vous permettre d’intégrer facilement la régie pub Google Adsense à un site en ajax.

1/ HTML : juste définir un div qui va contenir l’iframe

<div id="ad_slot"></div>

2/ jQuery : la fonction qui va créer l’iframe et y insérer le code google adsense

	function refreshAd(el, width, height) {
    	var currentTime = new Date();
    	if ( timer_refreshAd ) clearTimeout(timer_refreshAd) ;
    	timer_refreshAd = setTimeout(function() {
	    	keywords = escape($("meta[name='keywords']").attr('content'));
                file_get_ad = "/path/get_ad.php" ;
	    	$(el).html('<iframe id="googlead" class="googlead" name="googlead" href="" src="/'+ file_get_ad  +'/?s=' + currentTime.getTime() + '&amp;amp;keywords=' + keywords + '&amp;amp;w='+width+'&amp;amp;h='+height+'" width="'+width+'" height="'+height+'" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></iframe>' ) ;
    	}, 10) ;
	}

3/ Php : la page qui va servir de source à l’iframe généré

<script type="text/javascript">
google_ad_client = "YOUR_AD_ID";
google_ad_slot = "THE AD SLOT";
google_ad_width = <?php echo $_GET['w']; ?> ;
google_ad_height = <?php echo $_GET['h']; ?>
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 

<div class="content">
<?php
echo urldecode($_GET['keywords']) ;
?>
</div>

4/ On appelle maintenant le refreshAd par jquery

	refreshAd($('#ad_slot'), 300, 250) ;

Et la pub s’affiche :)

Bye.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists

Un commentaire

LEAVE A COMMENT

Comments RSS Feed   TrackBack URL