How to load a conditional js file using modernizr now that yepnope is deprecated?
Asked Answered
P

1

7

What are some good practices for loading a conditional javascript file using modernizr now that yepnope and .load are deprecated in the latest version of modernizr.

Used to be able to use the .load function. http://modernizr.com/docs/#load

Modernizr.load({
  test: Modernizr.geolocation,
  yep : 'geo.js',
  nope: 'geo-polyfill.js'
});

Now .load is deprecated along with yepnope. https://github.com/SlexAxton/yepnope.js/

Reference for answer prior to yepnope being deprecated Loading Scripts Using Modernizr... Not Working

Passus answered 30/7, 2015 at 17:30 Comment(1)
2 months, still unanswered?Formic
R
0

You can use jQuery's getScript method. I think you could also use .fail instead of the else statement. Spent my morning figuring this out and have been trying to answer these to save people some time!

Something like this?

if (Modernizr.geolocation) {
    jQuery.getScript("geo.js")
        //it worked! do something!
        .done(function(){
            console.log('geo.js loaded');
        });
} else {
    jQuery.getScript("geo-polyfill.js")
        //it worked! do something!
        .done(function(){
            console.log('geo-polyfill.js loaded');
        });
}
Revisal answered 29/12, 2015 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.