Click here to Skip to main content
15,883,822 members
Articles / Web Development / HTML
Tip/Trick

Social Media Buttons - Responsive Design

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
11 Oct 2016CPOL 9.9K   3  
Turn off social media buttons on a mobile device for faster page loading

Introduction

Social media buttons and their related code, are important components on contemporary web pages, but they do slow down page load time - an important consideration for smartphone and tablet users. This code illustrates how to detect (most) mobile devices and disable social media button loading.

Background

This code uses the CSS @media rule and, for example, the AddThis social media icons and service.

Using the Code

Place this JavaScript code just before the </body> tag at the end of your page:

JavaScript
var isMobile = {
 Android: function() { return navigator.userAgent.match(/Android/i); },
 BlackBerry: function() { return navigator.userAgent.match(/BlackBerry/i); },
 iOS: function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); },
 Opera: function() { return navigator.userAgent.match(/Opera Mini/i); },
 Windows: function() { return navigator.userAgent.match(/IEMobile/i); },
 any: function() { return (isMobile.Android() || isMobile.BlackBerry() || 
                   isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); }
 };
if (! isMobile.any() ) {
 document.write("<table align='center' cellpadding='1' cellspacing='1' width='184'>");
 document.write(" <tr>");
 document.write("  <td height='34' width='180'>");
 document.write("   <div class='addthis_toolbox addthis_default_style addthis_32x32_style'>");
 document.write("    <a class='addthis_button_preferred_1'></a> 
   <a class='addthis_button_preferred_2'></a> <a class='addthis_button_preferred_3'></a>");
 document.write("    <a class='addthis_counter addthis_bubble_style'></a>");
 document.write("    </div>");
 var a = document.createElement("script");
 a.src = "http://s7.addthis.com/js/250/addthis_widget.js#pubid=<your AddThis Publisher ID>";
 document.body.appendChild(a);
 document.write("   </td>");
 document.write("  </tr>");
 document.write(" </table>");
 document.write("<div class='e'></div>");
 }

This code utilizes the AddThis social media buttons, but other social media buttons or services could be coded in a similar manner.

History

  • No changes or improvements to date.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Zeducorp
United States United States
I was born and raised in New Jersey, went to Chatham High School and Newark College of Engineering, where I majored in Computer Science and graduated with a BS in Engineering Science. I worked briefly for Esso Math and Systems; then I was drafted into the U.S. army in 1971, where I worked as a computer programmer at the Letterman Army Institute of Research in San Francisco, California. During this period, I pursued graduate studies at Golden Gate College. After returning home from the army, I returned to work at the organization which became known as the Mathematics and Computer Science department of Exxon Corporation. Years later, in 1983, I formed Zeducorp after resigning from Exxon. During the 1980s and 1990s, I designed and wrote software for IBM personal computers and Novell LANs in the Digital Research PL/I, Microsoft Visual Basic, and Data Access Dataflex languages. Today, most of my work involves designing and developing a broad spectrum of static and database-driven websites.

Comments and Discussions

 
-- There are no messages in this forum --