简体   繁体   中英

loading none AMD module before any other code in RequireJS?

How can I load a none AMD module before any other modules are loaded? I have a shim for the console.x but having trouble loading this before anything else.

also I wish to load it only if a condition is true - so in this case as it is a console shim, I want to test to see if 'console' is undefined, then only load the console shim if the condition is true.

to note I am using RequireJS and do have Modernizr to hand as well.

Thanks.

This demo shows a rudimentary way of doing what you want.

The following script:

http://jsfiddle.net/bZB9L/6/

<script>
if ("undefined" === typeof window["mozInnerScreenX"]) {
    document.writeln("<script src=http://jsbin.com/erevub/4></scr" + "ipt>");
}
</script>
<script>
alert(mozInnerScreenX);
</script>

imports from this script:

http://jsbin.com/erevub/4

window["mozInnerScreenX"] = window["mozInnerScreenX"] || (function () {
    return 999;
}());

I've used mozInnerScreenX as the shim property, as all browsers on my machine have console .

The first script block that uses document.writeln must be in a separate script block to the following script that uses the shim.

There are lots of reasons why document.writeln is a bad idea, but I don't know your full use case, so this is simplest.

I get an alert for 999 on IE and Chrome (shim value), and 1074.5 on Firefox (real value).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM