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> 
10
  • The answer here: stackoverflow.com/questions/15732847/… By am_ doesn't fit your needs? Commented Aug 27, 2014 at 12:08
  • @EphiGabay I edited my question with the answer that I tried. Commented Aug 27, 2014 at 12:19
  • Define “desktop”. Does the Acer Aspire Switch 10 count? How about the Microsoft Surface Pro 3? If not, why not? Commented Aug 27, 2014 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. Commented Aug 27, 2014 at 12:43
  • @YinYang: good stuff, but that doesn’t remove the need to define what a “mobile device” is. Commented Aug 27, 2014 at 12:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

That didn't work. It removes it from the desktop version as well, despite loading the script.
@YinYang: it’s almost as if you and Patrick haven’t agreed what “desktop” means.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.