Skip to content

Instantly share code, notes, and snippets.

@rakeshpai
Created January 19, 2013 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rakeshpai/4571053 to your computer and use it in GitHub Desktop.
Save rakeshpai/4571053 to your computer and use it in GitHub Desktop.
Code to replicate a potential bug with Firefox when using the crossorigin attribute on script tags. When using crossorigin="anonymous", if the server doesn't set access-control headers, Firefox doesn't evaluate the external script, even though it's downloaded. Thus, alert(foo) breaks with a ReferenceError if the server doesn't send the access-co…
window.foo = 3;
<!doctype html>
<script src="http://localhost:4000/foo.js" crossorigin="anonymous"></script>
<script>
alert(foo);
</script>
var http = require("http"),
fs = require("fs");
http.createServer(function(req, res) {
// Only serves foo.js
res.setHeader("Access-Control-Allow-Origin", "*"); // Commenting this line out makes the loading of scripts asynchronous
res.setHeader("Content-Type", "application/javascript");
fs.createReadStream(__dirname + "/foo.js").pipe(res);
}).listen(4000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment