docs/v2/annotated-source/browser.html
browser.coffeecake.coffeecoffeescript.coffeecommand.coffeegrammar.coffeehelpers.coffeeindex.coffeelexer.coffeenodes.coffeeoptparse.coffeeregister.coffeerepl.coffeerewriter.coffeescope.litcoffeesourcemap.litcoffee
This Browser compatibility layer extends core CoffeeScript functions to make things work smoothly when compiling code directly in the browser. We add support for loading remote Coffee scripts via XHR , and text/coffeescript script tags, source maps via data-URLs, and so on.
CoffeeScript =require'./coffeescript'{ compile } = CoffeeScript
Use window.eval to evaluate code, rather than just eval, to run the script in a clean global scope rather than inheriting the scope of the CoffeeScript compiler. (So that cake test:browser also works in Node, use either window.eval or global.eval as appropriate).
CoffeeScript.eval=(code, options = {}) -\>options.bare ?=onglobalRoot =ifwindow?thenwindowelseglobal
globalRoot['eval'] compile code, options
Running code does not provide access to this scope.
CoffeeScript.run =(code, options = {}) -\>options.bare =onoptions.shiftLine =onFunction(compile code, options)()
Export this more limited CoffeeScript than what is exported by index.coffee, which is intended for a Node environment.
module.exports= CoffeeScript
If we’re not in a browser environment, we’re finished with the public API.
returnunlesswindow?
Include source maps where possible. If we’ve got a base64 encoder, a JSON serializer, and tools for escaping unicode characters, we’re good to go. Ported from https://developer.mozilla.org/en-US/docs/DOM/window.btoa
ifbtoa?andJSON?compile = (code, options = {}) -\>options.inlineMap =trueCoffeeScript.compile code, options
Load a remote script from the current domain via XHR.
CoffeeScript.load =(url, callback, options = {}, hold = false) -\>options.sourceFiles = [url]
xhr =ifwindow.ActiveXObjectnewwindow.ActiveXObject('Microsoft.XMLHTTP')elsenewwindow.XMLHttpRequest()
xhr.open'GET', url,truexhr.overrideMimeType'text/plain'if'overrideMimeType'ofxhr
xhr.onreadystatechange =-\>ifxhr.readyStateis4ifxhr.statusin[0,200]
param = [xhr.responseText, options]
CoffeeScript.run param...unlessholdelsethrownewError"Could not load #{url}"callback paramifcallback
xhr.sendnull
Activate CoffeeScript in the browser by having it compile and evaluate all script tags with a content-type of text/coffeescript. This happens on page load.
CoffeeScript.runScripts =-\>scripts = window.document.getElementsByTagName'script'coffeetypes = ['text/coffeescript','text/literate-coffeescript']
coffees = (sforsinscriptswhens.typeincoffeetypes)
index =0 execute = -\>param = coffees[index]ifparaminstanceofArrayCoffeeScript.run param...
index++
execute()forscript, iincoffeesdo(script, i) ->
options = literate: script.typeiscoffeetypes[1]
source = script.srcorscript.getAttribute('data-src')ifsource
options.filename = source
CoffeeScript.load source,(param) -\>coffees[i] = param
execute()
optionstrueelse
options.filename defines the filename the source map appears as in Developer Tools. If a script tag has an id, use that as the filename; otherwise use coffeescript, or coffeescript1 etc., leaving the first one unnumbered for the common case that there’s only one CoffeeScript script block to parse.
options.filename =ifscript.idandscript.idisnt''thenscript.idelse"coffeescript#{if i isnt 0 then i else ''}"options.sourceFiles = ['embedded']
coffees[i] = [script.innerHTML, options]
execute()
Listen for window load, both in decent browsers and in IE. Only attach this event handler on startup for the non-ES module version of the browser compiler, to preserve backward compatibility while letting the ES module version be importable without side effects.
ifthisiswindowifwindow.addEventListener
window.addEventListener'DOMContentLoaded', CoffeeScript.runScripts,noelsewindow.attachEvent'onload', CoffeeScript.runScripts