Saturday 27 July 2013

Simple But Best Tips Load Social Widgets Conditionally to Improve your Site’s Performance

Now days Social Sharing is now tightly integrated into all mobile OS platforms. That means if someone is viewing a web page on an Android phone, or inside the mobile Safari browser of an iPhone, they can share that web page on Twitter and Facebook using the system-wide sharing menu without having to rely on bookmark-lets or those tiny social sharing widgets.


These “tweet” and “like” buttons add several extra kilobytes to your web pages and if mobile users are less likely to use them, you may as well consider removing the social buttons completely when your website is accessed from a mobile phone. This reduces clutter and also improves the page speed score which is now a factor in Google search rankings.

Remove the Social Buttons on Mobile Devices
If you are sort-of convinced that social sharing widgets are less useful on your mobile site, here’s what you can do to prevent them from loading on the small screen. Remember, we aren’t just hiding the buttons but removing them completely so the associated JavaScript and CSS files won’t download on the user’s mobile device.

 How to Load Social Widgets Conditionally
 The technique is simple. We calculate the width of the user’s screen /browser and if the width exceeds a particular value (say 480 pixels), we load the associated social widgets.

To implement conditional loading on your blog /website, first add all the necessary social widgets to your blog using the usual procedure. For instance, you can visit facebook.com to generate code for the Like button while dev.twitter.com will provides codes the Tweet and Follow widgets of Twitter.

Remove the JavaScript from these generated codes – everything that’s between the <script> tags – and add everything else to your website template. Then copy-paste the following snippet before the closing </body> tag of your website template.
 

  1. <script type="text/javascript">


  2. (function(doc, script, minimum) {

  3.  

  4. // Calculate the width of the user's screen

  5. var browserWidth = window.innerWidth

  6. || doc.documentElement.clientWidth

  7. || doc.body.clientWidth;

  8.  

  9. // Load JavaScript only if the site is being viewed on a wide (non-mobile) screen

  10. if (browserWidth > minimum) {

  11.  

  12. var js, frag = doc.createDocumentFragment(),


  13. // Credit : Stoyan Stefanov of phpied.com

  14. js_queue = function(url, id) {

  15. if ( ! doc.getElementById(id) ) {

  16. js = doc.createElement(script);

  17. js.src = url; js.id = id;

  18. frag.appendChild(js);

  19. }

  20. };


  21. // These are the most common social widgets, remove the ones you don't need

  22. js_queue ("https://apis.google.com/js/plusone.js", "googleplus-js");

  23. js_queue ("//platform.twitter.com/widgets.js", "twitter-wjs");

  24. js_queue ("//connect.facebook.net/en_US/all.js#xfbml=1","facebook-jssdk");

  25. js_queue ("//platform.linkedin.com/in.js", "linkedin-js");

  26. js_queue ("//assets.pinterest.com/js/pinit.js", "pinterest-js");

  27.  

  28. var fjs = doc.getElementsByTagName(script)[0];

  29. fjs.parentNode.insertBefore(frag, fjs);

  30. }

  31.  

  32. // Set the minimum width here (default is 480 pixels)

  33. } ( document, 'script', 480 ) );

  34.  

  35. </script>


    I have implemented a variation of the above code on my site as well. There’s a Facebook Like box in the right sidebar but it won’t load if you open this site on a mobile device.

    If you like my work; you can show your regards by hitting Facebook like button, following us on Google+ or Twitter, stumbling our posts on stumble upon . Stay tuned for more tech updates.

    No comments:

    Post a Comment