MyBB Internal: One or more warnings occured. Please contact your administrator for assistance.
Asynchronous Javascript
Current time: 06-01-2020, 04:22 PM Hello There, Guest! (LoginRegister)

Post Reply 
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Asynchronous Javascript
08-17-2012, 01:09 AM
Post: #1
Asynchronous Javascript
In order to reduce page load times I want to asynchronously load all the Facebook/Twitter rubbish that now seems obligatory on a webpage. I have tried to do that by putting it all in a php include placed near the bottom of every page. The code looks like this:


<!-- Facebook -->
<div id="fb-root"></div>
<script>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=XXXXXXXXXXXXXXX";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>

<!-- Google +1 -->
<script type="text/javascript">
window.___gcfg = {lang: 'en-GB'};
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>

<!-- Twitter -->
<script>
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
</script>

<!-- Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXXX']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

Unfortunately it doesn't work. Angry

Without that code, my page loads in about 1.9 seconds. With the extra code it takes 5 seconds, and WebPageTest clearly shows that the various Google/Facebook/Twitter files are loading early in the page load process and consequently slowing the whole thing down.

Am I doing something wrong? Well, obviously I am, but what? Please be gently with me: I'm a javascript virgin. Angel
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Asynchronous Javascript - trapezeartist - 08-17-2012 01:09 AM
RE: Asynchronous Javascript - pmeenan - 08-17-2012, 11:36 PM
RE: Asynchronous Javascript - pmeenan - 08-18-2012, 06:11 AM

Forum Jump:


User(s) browsing this thread: 1 Guest(s)