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

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

  •  14-06-2023
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top