diff --git a/src/browser.coffee b/src/browser.coffee index 0c5d703d..adf72815 100644 --- a/src/browser.coffee +++ b/src/browser.coffee @@ -32,7 +32,7 @@ if btoa? and JSON? and unescape? and encodeURIComponent? "#{js}\n//@ sourceMappingURL=data:application/json;base64,#{btoa unescape encodeURIComponent v3SourceMap}\n//@ sourceURL=coffeescript" # Load a remote script from the current domain via XHR. -CoffeeScript.load = (url, callback = (->), options = {}, hold = false) -> +CoffeeScript.load = (url, callback, options = {}, hold = false) -> options.sourceFiles = [url] xhr = if window.ActiveXObject new window.ActiveXObject('Microsoft.XMLHTTP') @@ -43,13 +43,11 @@ CoffeeScript.load = (url, callback = (->), options = {}, hold = false) -> xhr.onreadystatechange = -> if xhr.readyState is 4 if xhr.status in [0, 200] - run = -> - CoffeeScript.run xhr.responseText, options - null + param = [xhr.responseText, options] + CoffeeScript.run param... unless hold else throw new Error "Could not load #{url}" - run() unless hold - callback run + callback param if callback xhr.send null # Activate CoffeeScript in the browser by having it compile and evaluate @@ -59,31 +57,30 @@ runScripts = -> scripts = window.document.getElementsByTagName 'script' coffeetypes = ['text/coffeescript', 'text/literate-coffeescript'] coffees = (s for s in scripts when s.type in coffeetypes) + index = 0 - check = -> - for run, index in coffees - continue unless run - return unless run instanceof Function - coffees[index] = run() + execute = -> + param = coffees[index] + if param instanceof Array + CoffeeScript.run param... + index++ + execute() - for script, index in coffees - do (script, index) -> + for script, i in coffees + do (script, i) -> options = literate: script.type is coffeetypes[1] if script.src CoffeeScript.load script.src, - (run) -> - coffees[index] = run - check() + (param) -> + coffees[i] = param + execute() options true else options.sourceFiles = ['embedded'] - coffees[index] = -> - CoffeeScript.run script.innerHTML, options - null + coffees[i] = [script.innerHTML, options] - check() - null + execute() # Listen for window load, both in decent browsers and in IE. if window.addEventListener