How to use Modernizr in head to conditionally load JS - TypeError produced

StackOverflow https://stackoverflow.com/questions/22400299

  •  14-06-2023
  •  | 
  •  

Question

After loading Modernizr in my page's head, I'm trying to test for a feature - if it fails it need to load some JavaScript. However, I'm getting the following console error:

TypeError: 'undefined' is not a function (evaluating 'Modernizr.load([{
        test : Modernizr.csstransforms,
        nope : '/path/to/polyfill.js'
    }
])')

My <head> section looks like:

<head>
    ...
    <script src="path/to/modernizr.js"></script>

    <script>
    Modernizr.load([{
        test : Modernizr.csstransforms,
        nope : '/path/to/polyfill.js'
    }
    ]);
    </script>
</head>

How do I correctly conditionally load in the head?

Était-ce utile?

La solution

It looks like you didn't have Modernizr.load checked off in your build of modernizr. That being said, Modernizr.load has been removed from the pre-release version of modernizr, becuase it was just a thin wrapper around yepnope. You can rebuild your build with .load included, but I would highly suggest skipping that and just loading yepnope instead.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top