Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just load your adsense code by AJAX. First prepare on your page box for ads placement:</p> <pre><code>&lt;div id="adsense-box"&gt;&lt;/div&gt; </code></pre> <p>Now, check user browser resolution and depending on result, do proper AJAX request for Ads HTML. This example show how you can achieve it with <a href="http://jquery.com/" rel="nofollow">jQuery</a> (<a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">.ajax </a>function):</p> <p>JavaScript:</p> <pre><code>if ( isMobileResolution ) { url = "/mobile_adsense.php"; } else { url = "/website_adsesne.php"; } $.ajax({ url: url }).done(function(content) { $("#adsense-box").html(content); }); </code></pre> <p><strong>Other solution,</strong> without PHP:</p> <p>Just send your Adsense codes not to HTML elements, but to Javascript variable, like this:</p> var codemobile = ""; var codewebsite = ""; <p>than, you can append to any HTML element one of those codes, after resolution detection. Because AdSense code will not be in document DOM HTML, it will not be loaded by google and treated as "executed" - those codes will be just waiting in varaibles. Remember to escale <code>"</code> signs etc.</p> <p>To catch those codes to a variable, you can swich echo with $var .= or user PHP buffering (ob_start() etc). This solutions saves you request and latency with AJAX.</p> <p><strong>Third solution - document.write safe</strong></p> <p>Salman A. pointed that AdSense uses document.write, so maybe prepare iframes on site, resize those iframes after resolution detections, and set src to proper .php file.</p> <p>I don't know how adsense will work in iframe - be aware that contextual ad placement can be broken because of lack of content on page (iframe).</p> <p><strong>Another soluction - totally safe, but with additional refresh/empty landing page</strong></p> <p>You can check in JavaScript if resolution is small, and set a cookie for ad selection, and refresh a page if cookie was not previously set. Let's PHP generate proper ads code dependly on cookie value.</p> <p>This solution must work but you will have single additional refresh when user is on mobile device &amp; entering page for the first time. If you already have some landing or redirection page, maybe try to set cookies there.</p> <p>You can also switch to mobile-ads-default and trait unusual with refresh only standard users, if this page is view more frequently from mobile devices</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload