If you are a website owner like me then you might have known the fact that Google nowadays emphasizes on having speedy websites. You will have a great chance to rank higher in Google search if your website opens faster than your competitors.
You might have done several tweaks to improve the site speed for better experience of your users and you might have used either Google Page Speed Checker or Gtmetrix to check the website speed.
Many people face this javascript defer problem .

So, today I am going to provide you solution to defer your external javascript loading.
Use the following code between the head tags in your website.
<script>
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>For Google Plus :
<script>
(function(d, t) {
var g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.src = '//apis.google.com/js/plusone.js';
s.parentNode.insertBefore(g, s);
}(document, 'script'));
</script>For Twitter :
<script>
(function(d, t) {
var g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.src = '//platform.twitter.com/widgets.js';
s.parentNode.insertBefore(g, s);
}(document, 'script'));
</script>For any external javascript :
<script>
(function(d, t) {
var g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.src = 'Your external javascript code';
s.parentNode.insertBefore(g, s);
}(document, 'script'));
</script>







Hi,
Currently am using W3TC plugin to speed the load time of my website. Regarding the facebook and +1 buttons am using plugins. So will you recommend me to remove the javascripts from those plugins and then install the codes given by you in the header.php ???
I was also thinking about some ways to follow to speed up my site for getting better time on site and search engine ranking.
Thanks for the scripts.
Now this are pretty nice set of codes. Thanks for the share
I never read about Google’s policy to rank high who are speedy but you opened my eyes. Thanks for these codes. will surely try them and add necessary plugins for making my web speedy.
I am really having a problem with my site’s loading speeds and I was right about it having to do with my Javascript codes. Thanks for posting these great code snippets. I can really see the improvement in my site’s loadings speeds.
To be honest reading Javascript is so much complicated, but I really admire your courage in sharing this codes. to speed up out own site.
Thanks for helping me find out why my website is taking slow to load. Helped a lot.
I love how you explain the process on fixing the site.
i will try this trick hope my website speed will be improved and get more visitors. :)
How timely, i was having a problem with my affiliate site that loading very
slow and I couldn’t find the answer how it make faster. thanks for the info.
Thanks so much I have been using a caching plugin but will certainly give your code a try.
I’ve tried this trick and have been successful, at least there is little change on my site. Thank you very much
Thanks a lot for this tip. I am owner of blog and having same problem regarding loading time of website and today I got solution .
YES!! This really helped a lot. Thanks so much.
I guess this answered my problem, thanks.
Glad I found this info. Really helped a lot.