I want to load modernizr synchronously in head to prevent a fouc. I am using require.js at before /body to load some other scripts in which I'd like to use modernizr for feature detection and such things.
What is the right way to do this or is it even remended to do so? If I require modernizr in my scripts it gets loaded again, if I won't, it is undefined.
Thanks in advance. :)
I want to load modernizr synchronously in head to prevent a fouc. I am using require.js at before /body to load some other scripts in which I'd like to use modernizr for feature detection and such things.
What is the right way to do this or is it even remended to do so? If I require modernizr in my scripts it gets loaded again, if I won't, it is undefined.
Thanks in advance. :)
Share Improve this question edited May 2, 2013 at 7:34 shapeshifta asked Apr 29, 2013 at 13:28 shapeshiftashapeshifta 2985 silver badges16 bronze badges1 Answer
Reset to default 9If Modernizr is the 1st script loaded in head, then it is accessible from everywhere, so you can define simple wrapper like this:
define('modernizr', function () { return window.Modernizr });
This code may be put inside wrappers.js
like this:
<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>
Then in 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
})
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742300406a4417926.html
评论列表(0条)