The Joomla! Forum ™



Forum rules


Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Security and Performance FAQs
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Jul 21, 2011 6:39 pm 
Joomla! Explorer
Joomla! Explorer

Joined: Mon Aug 11, 2008 3:05 pm
Posts: 459
Putting javascript in the middle of the page slows down your start to render time. Here is my solution to have adsense or anything else load last.

(1) Create another position in your template's index.php file after the </html> tag.
Code:
</html>
<jdoc:include type="modules" name="load_last" />


Then using mod_custom create two objects. The first on is just a placeholder, in my case it's in the left column.

(2) Use the edit code and simply place this into the edit box and save it in the left column position.
Code:
<div id="adsense_left_1_placeholder"></div>


(3) Then use mod_custom to create another object using the following code and then set it's position to "load_last".
Code:
<div id="adsense_left_1" style="margin: 12px;">
<script type="text/javascript">
   // <![CDATA[
   google_ad_client = "pub-xxxxxxxx42314842";
   /* 160x600, created 6/19/11 */
   google_ad_slot = "xxxxxxxx65";
   google_ad_width = 160;
   google_ad_height = 600;
   // ]]>
</script>
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
</div>

<div>
<script type="text/javascript">
   // <![CDATA[
   try
   {
document.getElementById('adsense_left_1').parentNode.setAttribute("id","adsense_left_1_parent"); 

document.getElementById('adsense_left_1_placeholder').parentNode.appendChild(document.getElementById('adsense_left_1'));       
      if (document.all)
      {
         d=document.getElementById('adsense_left_1_parent');
         d.style.visibility='hidden';
      }
      else
      {
         d=document.getElementById('adsense_left_1_parent');
         d.parentNode.removeChild(d);
      }
      d=document.getElementById('adsense_left_1_placeholder');
      d.parentNode.removeChild(d);
   }
   catch(err)
   {
   }
   // ]]>
</script>
</div>


Once the code runs it changes the position of the adsense code and then cleans up the unused objects.

For some reason IE gives a nice "Operation Aborted" error when it tries to remove the "adsense_left_1_parent" object so for now I'm just hiding it until I find something that works in IE.


Top
 Profile  
 
PostPosted: Sun May 06, 2012 10:23 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Mar 24, 2011 11:45 pm
Posts: 10
some years later... ;)

has anyone found a solution for getting something like that working with IE?

i'm trying to find out somthing like that for the Google Custrom Search jsapi. The script should be loaded after everything else as it especially takes some time in IE for the content to show up...


Top
 Profile  
 
PostPosted: Mon May 07, 2012 1:09 am 
Joomla! Explorer
Joomla! Explorer

Joined: Mon Aug 11, 2008 3:05 pm
Posts: 459
That method also works for IE, you just can't d.parentNode.removeChild(d) but you can d.style.visibility='hidden';


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 



Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group