Created
January 30, 2012 22:18
-
-
Save meelash/1707155 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Attaches the script represented by the URL to the current | |
| * environment. Right now only supports browser loading, | |
| * but can be redefined in other environments to do the right thing. | |
| * @param {String} url the url of the script to attach. | |
| * @param {Object} context the context that wants the script. | |
| * @param {moduleName} the name of the module that is associated with the script. | |
| * @param {Function} [callback] optional callback, defaults to require.onScriptLoad | |
| * @param {String} [type] optional type, defaults to text/javascript | |
| * @param {Function} [fetchOnlyFunction] optional function to indicate the script node | |
| * should be set up to fetch the script but do not attach it to the DOM | |
| * so that it can later be attached to execute it. This is a way for the | |
| * order plugin to support ordered loading in IE. Once the script is fetched, | |
| * but not executed, the fetchOnlyFunction will be called. | |
| */ | |
| req.attach = function (url, context, moduleName, callback, type, fetchOnlyFunction) { | |
| var node; | |
| if (isBrowser) { | |
| //In the browser so use a script tag | |
| callback = callback || req.onScriptLoad; | |
| node = context && context.config && context.config.xhtml ? | |
| document.createElementNS("http://www.w3.org/1999/xhtml", "html:script") : | |
| document.createElement("script"); | |
| node.type = type || (context && context.config.scriptType) || | |
| "text/javascript"; | |
| node.charset = "utf-8"; | |
| //Use async so Gecko does not block on executing the script if something | |
| //like a long-polling comet tag is being run first. Gecko likes | |
| //to evaluate scripts in DOM order, even for dynamic scripts. | |
| //It will fetch them async, but only evaluate the contents in DOM | |
| //order, so a long-polling script tag can delay execution of scripts | |
| //after it. But telling Gecko we expect async gets us the behavior | |
| //we want -- execute it whenever it is finished downloading. Only | |
| //Helps Firefox 3.6+ | |
| //Allow some URLs to not be fetched async. Mostly helps the order! | |
| //plugin | |
| node.async = !s.skipAsync[url]; | |
| if (context) { | |
| node.setAttribute("data-requirecontext", context.contextName); | |
| } | |
| node.setAttribute("data-requiremodule", moduleName); | |
| //Set up load listener. Test attachEvent first because IE9 has | |
| //a subtle issue in its addEventListener and script onload firings | |
| //that do not match the behavior of all other browsers with | |
| //addEventListener support, which fire the onload event for a | |
| //script right after the script execution. See: | |
| //https://connect.microsoft.com/IE/feedback/details/648057/script-onload-event-is-not-fired-immediately-after-script-execution | |
| //UNFORTUNATELY Opera implements attachEvent but does not follow the script | |
| //script execution mode. | |
| if (node.attachEvent && !isOpera) { | |
| //Probably IE. IE (at least 6-8) do not fire | |
| //script onload right after executing the script, so | |
| //we cannot tie the anonymous define call to a name. | |
| //However, IE reports the script as being in "interactive" | |
| //readyState at the time of the define call. | |
| useInteractive = true; | |
| if (fetchOnlyFunction) { | |
| //Need to use old school onreadystate here since | |
| //when the event fires and the node is not attached | |
| //to the DOM, the evt.srcElement is null, so use | |
| //a closure to remember the node. | |
| node.onreadystatechange = function (evt) { | |
| //Script loaded but not executed. | |
| //Clear loaded handler, set the real one that | |
| //waits for script execution. | |
| if (node.readyState === 'loaded') { | |
| node.onreadystatechange = null; | |
| node.attachEvent("onreadystatechange", callback); | |
| fetchOnlyFunction(node); | |
| } | |
| }; | |
| } else { | |
| node.attachEvent("onreadystatechange", callback); | |
| } | |
| } else { | |
| node.addEventListener("load", callback, false); | |
| } | |
| node.src = url; | |
| //Fetch only means waiting to attach to DOM after loaded. | |
| if (!fetchOnlyFunction) { | |
| req.addScriptToDom(node); | |
| } | |
| return node; | |
| } else if (isWebWorker) { | |
| //In a web worker, use importScripts. This is not a very | |
| //efficient use of importScripts, importScripts will block until | |
| //its script is downloaded and evaluated. However, if web workers | |
| //are in play, the expectation that a build has been done so that | |
| //only one script needs to be loaded anyway. This may need to be | |
| //reevaluated if other use cases become common. | |
| importScripts(url); | |
| //Account for anonymous modules | |
| context.completeLoad(moduleName); | |
| } | |
| return null; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment