Skip to content

Commit

Permalink
For the ES module version of the browser compiler, don't automaticall…
Browse files Browse the repository at this point in the history
…y attach the runScripts event handler
  • Loading branch information
GeoffreyBooth committed Mar 25, 2019
1 parent 3c6d609 commit ad7505f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 18 deletions.
15 changes: 10 additions & 5 deletions docs/v2/annotated-source/browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,19 @@ <h1>browser.coffee</h1>
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">&#182;</a>
</div>
<p>Listen for window load, both in decent browsers and in IE.</p>
<p>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.</p>

</div>

<div class="content"><div class='highlight'><pre><span class="hljs-keyword">if</span> <span class="hljs-built_in">window</span>.addEventListener
<span class="hljs-built_in">window</span>.addEventListener <span class="hljs-string">'DOMContentLoaded'</span>, CoffeeScript.runScripts, <span class="hljs-literal">no</span>
<span class="hljs-keyword">else</span>
<span class="hljs-built_in">window</span>.attachEvent <span class="hljs-string">'onload'</span>, CoffeeScript.runScripts</pre></div></div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">if</span> <span class="hljs-keyword">this</span> <span class="hljs-keyword">is</span> <span class="hljs-built_in">window</span>
<span class="hljs-keyword">if</span> <span class="hljs-built_in">window</span>.addEventListener
<span class="hljs-built_in">window</span>.addEventListener <span class="hljs-string">'DOMContentLoaded'</span>, CoffeeScript.runScripts, <span class="hljs-literal">no</span>
<span class="hljs-keyword">else</span>
<span class="hljs-built_in">window</span>.attachEvent <span class="hljs-string">'onload'</span>, CoffeeScript.runScripts</pre></div></div>

</li>

Expand Down
14 changes: 10 additions & 4 deletions docs/v2/browser-compiler-modern/coffeescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -11200,10 +11200,16 @@ if (typeof module !== 'undefined' && require.main === module) {
};

// Listen for window load, both in decent browsers and in IE.
if (window.addEventListener) {
window.addEventListener('DOMContentLoaded', CoffeeScript.runScripts, false);
} else {
window.attachEvent('onload', CoffeeScript.runScripts);
// 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.
if (this === window) {
if (window.addEventListener) {
window.addEventListener('DOMContentLoaded', CoffeeScript.runScripts, false);
} else {
window.attachEvent('onload', CoffeeScript.runScripts);
}
}

}).call(this);
Expand Down
2 changes: 1 addition & 1 deletion docs/v2/browser-compiler/coffeescript.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/v2/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<script type="module">
import CoffeeScript from './browser-compiler-modern/coffeescript.js';
window.CoffeeScript = CoffeeScript;
window.addEventListener('DOMContentLoaded', CoffeeScript.runScripts, false);
</script>
<script src="https://cdn.jsdelivr.net/underscorejs/1.8.3/underscore-min.js"></script>
<style>
Expand Down
1 change: 1 addition & 0 deletions documentation/site/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<script type="module">
import CoffeeScript from './browser-compiler-modern/coffeescript.js';
window.CoffeeScript = CoffeeScript;
window.addEventListener('DOMContentLoaded', CoffeeScript.runScripts, false);
</script>
<script src="https://cdn.jsdelivr.net/underscorejs/1.8.3/underscore-min.js"></script>
<style>
Expand Down
14 changes: 10 additions & 4 deletions lib/coffeescript/browser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions src/browser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ CoffeeScript.runScripts = ->
execute()

# Listen for window load, both in decent browsers and in IE.
if window.addEventListener
window.addEventListener 'DOMContentLoaded', CoffeeScript.runScripts, no
else
window.attachEvent 'onload', CoffeeScript.runScripts
# 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.
if this is window
if window.addEventListener
window.addEventListener 'DOMContentLoaded', CoffeeScript.runScripts, no
else
window.attachEvent 'onload', CoffeeScript.runScripts

0 comments on commit ad7505f

Please sign in to comment.