<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>piouPiouM&#039;s dev&#187; Archives pour le tag optimiser – piouPiouM&#039;s dev</title>
	<atom:link href="http://pioupioum.fr/tag/optimiser/feed/" rel="self" type="application/rss+xml" />
	<link>http://pioupioum.fr</link>
	<description>Bloc-note d&#039;un développeur web</description>
	<lastBuildDate>Thu, 05 Aug 2010 13:48:33 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Optimiser le chargement des AdSense</title>
		<link>http://pioupioum.fr/developpement/optimiser-rapidite-chargement-adsense-jquery.html</link>
		<comments>http://pioupioum.fr/developpement/optimiser-rapidite-chargement-adsense-jquery.html#comments</comments>
		<pubDate>Thu, 11 Mar 2010 16:48:57 +0000</pubDate>
		<dc:creator>piouPiouM</dc:creator>
				<category><![CDATA[Développement Web]]></category>
		<category><![CDATA[astuces]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[optimiser]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://pioupioum.fr/?p=357</guid>
		<description><![CDATA[Il n&#8217;y a rien de plus horripilant que d&#8217;attendre que des publicités chargent pour accéder au contenu d&#8217;un article. C&#8217;est malheureusement une situation que l&#8217;on rencontre fréquemment. La faute en incombe au fonctionnement des blocs AdSense des publicitaires.

En effet, ils nécessitent que nous insérerions le code correspondant à un bloc à l&#8217;endroit où il sera [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>Il n&#8217;y a rien de plus horripilant que d&#8217;attendre que des publicités chargent pour accéder au contenu d&#8217;un article. C&#8217;est malheureusement une situation que l&#8217;on rencontre fréquemment. La faute en incombe au fonctionnement des blocs <strong>AdSense</strong> des publicitaires.</p>

<p>En effet, ils nécessitent que nous insérerions le code correspondant à un bloc à l&#8217;endroit où il sera affiché. Cela pour deux raisons notamment&#160;:</p>

<ol>
<li>Des variables globales identifiants l&#8217;annonce sont déclarées.</li>
<li>Le script qui va charger l&#8217;annonce est immédiatement exécuté et fait appel à des actions d&#8217;écriture dans le flux <em>via</em> <a href="https://developer.mozilla.org/fr/DOM/document.write">write</a>.</li>
</ol>

<p>Le deuxième point oblige le navigateur à traiter le code de l&#8217;adsense dès qu&#8217;il le rencontre puisque ce dernier n&#8217;utilise pas de gestionnaire de chargement. En plus d&#8217;écrire dans le flux courant, le système du publicitaire va charger des éléments extérieurs. Pour peu que la latence des réponses soit élevée, l&#8217;impression de <strong>ralentissement du chargement de la page</strong> augmente.</p>

<p>S&#8217;est ainsi que si un article est précédé d&#8217;un encart publicitaire, l&#8217;utilisateur devra patiemment attendre le chargement de ce dernier avant de pouvoir accéder au contenu désiré.<br />
L&#8217;expérience utilisateur s&#8217;en trouve dégradée.</p>

<h2>En finir avec les ralentissements de chargement des pages</h2>

<p>Vous l&#8217;aurez peut-être remarqué, mais sur <a href="http://pioupioum.fr/" title="piouPiouM&#8217;s dev : bloc-note d&#8217;un développeur web. PHP, git, shell, plugins WordPress, SPIP, jQuery">piouPiouM&#8217;s dev</a>, le <strong>chargement des blocs publicitaires</strong> est comme <strong>temporisé</strong>, ne gênant ainsi en rien la navigation sur le site.<br />
Voyons dans la suite du billet comment y parvenir, en prenant pour exemple le service <a href="https://www.google.com/adsense/">Google Adsense</a>.
<span id="more-357"></span></p>

<h2 id="integration-maquette">Intégration dans une maquette</h2>

<p>On commence par définir les emplacements des encarts publicitaires. Cela vous permet ainsi d&#8217;intégrer les annonces dans vos maquettes <abbr title="Hyper Text Markup Language">HTML</abbr>, tout en vous assurant que cette apparence sera conservée pour les utilisateurs n&#8217;ayant pas JavaScript d&#8217;activé, ou qui bloquent les annonces publicitaires.<br />
Je prend ici le parti d&#8217;annoncer la couleur, en précisant la mention <em>Publicité</em>.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;ads-[ads_id]&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ads [ads_format]&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    Publicité
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<p>En sommes, il s&#8217;agit d&#8217;un simple élément <code>&lt;div&gt;</code> ayant pour attributs&#160;:</p>

<ul>
<li>un identifiant <strong>[ads_id]</strong>&#160;: un identifiant unique qui va servir de point d&#8217;ancrage au contenu de l&#8217;adsense. Vous pouvez par exemple, dans le cas de Google Adsense, utiliser le nom du critère personnalisé en notation CamelCase.</li>
<li>une classe <strong>ads</strong>&#160;: le style des encarts de pub lorsque celles-ci sont en attente de chargement.</li>
<li>une classe <strong>[ads_format]</strong>&#160;: le format de l&#8217;encart publicitaire. Par exemple, <code>square250</code> pour une annonce de 250px de côtés ou encore <code>banner468</code> pour une bannière au format 468 x 60.</li>
</ul>

<p>Appliquez les styles <abbr title="Cascading Style Sheets">CSS</abbr> qui suivent.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* @group Tools */</span>
&nbsp;
<span style="color: #6666ff;">.hide</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* @end   Tools */</span>
<span style="color: #808080; font-style: italic;">/* @group Adsense */</span>
&nbsp;
<span style="color: #6666ff;">.ads</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#F4F4F4</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#666666</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">11px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">text-transform</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">uppercase</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.square250</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">250px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">250px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">250px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.banner468</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">60px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">60px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">468px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* @end   Adsense */</span></pre></td></tr></table></div>


<h2 id="ads-code-annonceur">Insertion du code de l&#8217;annonceur</h2>

<p>Insérez en fin de page les différents codes donnés par votre annonceur (ici, Google Adsense).</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;hide&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Ads --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;adsref-[ads_id]&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
google_ad_client = &quot;pub-[client_id]&quot;;
google_ad_slot   = &quot;[slot_id]&quot;;
google_ad_width  = 250;
google_ad_height = 250;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<p><strong>Chaque annonce</strong> est incluse dans un élément ayant pour identifiant <code>adsref-[ads_id]</code> (voir <a href="#integration-maquette">Intégration de la maquette</a>) qui fait le lien avec l&#8217;emplacement final de la publicité.</p>

<p>Le tout étant masqué à l&#8217;aide d&#8217;un <code>div.hide</code>. Il serait en effet désagréable d&#8217;afficher les annonces publicitaires en bas de page, même si ce n&#8217;est que pour une poignée de [micro]secondes.</p>

<h2 id="jquery-ads-loader">Un Adsense Loader avec jQuery</h2>

<p>Il ne reste plus qu&#8217;à déplacer à l&#8217;aide de JavaScript les blocs AdSense aux endroits souhaités. Avec <a href="http://pioupioum.fr/tag/jquery/" title="Archives pour le tag jQuery – piouPiouM&#039;s dev">jQuery</a> cela donne&#160;:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> $ads<span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div[id^=&quot;adsref-&quot;]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>          <span style="color: #006600; font-style: italic;">// pour chaque bloc d'annonce #adsref-[ads_id]</span>
        $ads <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#ads-'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span>.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// supprimer de #ads-[ads_id] tout éventuel contenu</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ins:first'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span>$ads<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>           <span style="color: #006600; font-style: italic;">// déplacer l'annonce dans #ads-[ads_id]</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>Bien évidemment, et comme vous êtes sensible aux bonnes pratiques liées à la <a href="http://performance.survol.fr/" title="Performance web">performance web</a>, vous insérerez naturellement ce code en fin de page <img src='http://pioupioum.fr/wp/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>

<h3 class='related_post_title'>Continuez votre lecture sur des sujets similaires</h3>

<ul class='related_post'><li><a href='http://pioupioum.fr/developpement/javascript-array-intersection.html' title='JavaScript : optimiser le calcul de l&#8217;intersection de tableaux de grandes tailles'>JavaScript&#160;: optimiser le calcul de l&#8217;intersection de tableaux de grandes tailles</a></li><li><a href='http://pioupioum.fr/developpement/jslint-console-rhino.html' title='Une console JSLint pour aider vos validations JavaScript'>Une console JSLint pour aider vos validations JavaScript</a></li><li><a href='http://pioupioum.fr/developpement/jquery-color-plugin-animation-rgba-support.html' title='Du RGBA dans vos animations avec le plugin jQuery Color'>Du RGBA dans vos animations avec le plugin jQuery Color</a></li><li><a href='http://pioupioum.fr/developpement/javascript-detecter-support-rgba.html' title='JavaScript : tester le support des couleurs RGBA'>JavaScript&#160;: tester le support des couleurs RGBA</a></li><li><a href='http://pioupioum.fr/snippets/wordpress-autoriser-upload-media-format-inconnu.html' title='WordPress : autoriser l&#8217;upload de fichiers au format non-supporté'>WordPress&#160;: autoriser l&#8217;upload de fichiers au format non-supporté</a></li></ul>
<!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://pioupioum.fr/developpement/optimiser-rapidite-chargement-adsense-jquery.html/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
