0

I'm using an external JS plugin that loads very slowly on mobile devices and want to load it only on desktop.

My JS code is like this

<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-53a4066858ab24f0"></script>

I read the answers given here and here but I can't figure out how I can do this for an external JS file. Any ideas how to do this?

I tried this but this removed it from the desktop version as well, despite loading the script (monitored using Network resources in Chrome Dev Tools).

<script>
    (function ($) {
        if ($(window).width() >= 768) {
            url = '//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-53a4066858ab24f0';
            $.getScript(url);
        }
    })(jQuery);
</script>
Community
  • 1
  • 1
Yin Yang
  • 1,576
  • 2
  • 24
  • 45
  • The answer here: http://stackoverflow.com/questions/15732847/load-jquery-script-in-desktop-browser-but-not-mobile By am_ doesn't fit your needs? – Ephi Gabay Aug 27 '14 at 12:08
  • @EphiGabay I edited my question with the answer that I tried. – Yin Yang Aug 27 '14 at 12:19
  • Define “desktop”. Does [the Acer Aspire Switch 10](http://www.acer.co.uk/ac/en/GB/content/series/aspireswitch10) count? How about [the Microsoft Surface Pro 3](http://www.microsoft.com/surface/en-gb/products/surface-pro-3)? If not, why not? – Paul D. Waite Aug 27 '14 at 12:22
  • @PaulD.Waite I'm not that nitpicky over what qualifies as a "desktop". It's a social share plugin that wouldn't really affect the functionality of a website, if missing. As long as it is removed from most mobile devices, my question is solved. – Yin Yang Aug 27 '14 at 12:43
  • @YinYang: good stuff, but that doesn’t remove the need to define what a “mobile device” is. – Paul D. Waite Aug 27 '14 at 12:44
  • @PaulD.Waite For my purpose, device width less than `768px`? – Yin Yang Aug 27 '14 at 12:47
  • @YinYang: maybe. Is it the device’s small screen size that’s causing the JavaScript library to load slowly? – Paul D. Waite Aug 27 '14 at 12:48
  • @PaulD.Waite Could be. But the irritating part is that it loads the entire script but still doesn't work as it isn't compatible with mobile devices. – Yin Yang Aug 27 '14 at 12:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60087/discussion-between-paul-d-waite-and-yin-yang). – Paul D. Waite Aug 27 '14 at 13:21
  • If there’s something the script is incompatible with, then that could be something concrete you can detect before deciding whether to use the library. What’s the compatibility issue? – Paul D. Waite Aug 27 '14 at 13:44

1 Answers1

0

I believe that something like this would work for you:

<script>
var isDesktop = (function() {
  return !('ontouchstart' in window) || !('onmsgesturechange' in window);
})();
window.isDesktop = isDesktop;
if( isDesktop ){ url = '//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-53a4066858ab24f0';
                 $.getScript(url);  
               }
</script>

Found here: Load jQuery script in desktop browser, but not mobile By Travis J

Community
  • 1
  • 1
Patrick
  • 146
  • 13