0

I have a plugin.js that combine all my plugins for my responsive build. This plugin.js is unnecessarily bulky for mobile. I would like to create 2 plugin.js to switch between for mobile or desktop.

What is the best practice for reducing overhead for mobile scripting?

Examples: I have a complicated jQuery slider for desktop viewing but I would like that script not to load for mobile.

For this project i'll be using: HTML5, Wordpress, jQuery, Modernizr.

Armeen Harwood
  • 15,195
  • 30
  • 106
  • 210
  • For ways to detect mobile browsers from your JavaScript, you should take a look a few helpful answers to this question: http://stackoverflow.com/questions/3514784/best-way-to-detect-handheld-device-in-jquery – andypaxo Jul 04 '12 at 21:51

2 Answers2

1

It depends what you define as mobile. If it's all devices below a certain width, you can test for that, and then prevent the script running with an if statement.

$(function(){
    var mobile;
    if (window.width < 481) {
        mobile = 1; 
    }

    if (!mobile) {
    // All your stuff.
    }
});
Jezen Thomas
  • 13,004
  • 5
  • 49
  • 87
  • What would be best practice to detect for mobile? what about retina display? so many factors I feel like I would be missing something... is there a best practice for this situation? – Armeen Harwood Jul 04 '12 at 21:46
  • It seems that 'best practice' when building for mobile is up for debate, unfortunately. – Jezen Thomas Jul 04 '12 at 21:51
0

I would not recommend using JavaScript to check the device. What are you using for the server side? I have used the free PHP mobile detect class with much success. It can detect mobile tablet or specific manufacturer as well eg. iPad. It is super easy to use also. Then you could generate the page based on that or redirect to a specially formatted mobile site. using javascript is very unreliable and some people could have it turned off in their browser, not likely but it happens. If you do it on the server you dont have to worry about things like that.

John S
  • 533
  • 6
  • 22