Download the upgrade in your head, but use it in require.js

I want to load modernizr synchronously in my head to prevent fouc. I use require.js in before / body to load some other scripts in which I would like to use modernizr to detect functions and such things.

What is the right way to do this or is it even recommended to do this? If I need an upgrade in my scripts, it loads again, if not, it is undefined.

Thanks in advance.:)

+5
source share
1 answer

If Modernizr is the first script loaded to the head, then it is accessible from the outside, so you can define a simple shell like this:

define('modernizr', function () { return window.Modernizr });

wrappers.js :

<head>
<script src="/js/vendor/modernizr.js"></script>   
<script src="/js/vendor/require.js"></script>  
<script src="/js/wrappers.js"></script>  
<script src="/js/main.js"></script>
</head>

main.js

var scripts = document.getElementsByTagName('script')
  , src = scripts[scripts.length - 1].src
  , baseUrl = src.substring(src.indexOf(document.location.pathname), src.lastIndexOf('/'))

require.config({
  baseUrl: baseUrl
})
+9

All Articles