Detect load type in jQuery: ajax vs regular http
Is there a way of knowing if a jQuery mobile page has been loaded directly (regular http call) or though ajax in order to load Adsense?
Basically, Google Adsense cannot be loaded in pages that are loaded via ajax because it throws the error message:
Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
So the solution is to only display the Google Adsense banner if it is a regular http request. For that, I'm using this method:
- var banner = false;
- $(document).on('mobileinit', function(){
- banner = true;
- });
Of course, there is no way to actually use that "banner" flag with this kind of Adsense code:
- <script type="text/javascript">
- google_ad_client = "ca-pub-XXXXXXXXXXXXXX";
- google_ad_slot = "XXXXXXXX";
- google_ad_width = 320;
- google_ad_height = 100;
- </script>
- <script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js"></script>
and neither with the async method:
- <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-XXXXXXXXXXXX" data-ad-slot="XXXXXXX" data-ad-format="auto"></ins>
- <script>
- (adsbygoogle = window.adsbygoogle || []).push({});
- </script>
Any ideas? Thanks!