I know I am probably doing this wrong because if trying this through try coffeescript feature it works but surprisingly it doesn't emit any result on my example:
<!--http://f.cl.ly/items/1u3Q3W101U2T18162v0V/test.html-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Page Title</title>
<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script>
</head>
<body>
<script type="text/coffeescript" >
document.write "<h2>TEST</h2>"
</script>
</body>
</html>
The document.write
method doesn't seems to output anything to the body, in this case console.log works fine but not document.write
Even after trying to run the script with a onload
handler like I use in javascript
var loaded = function(){
alert("hello");
}
document.addEventListener('DOMContentLoaded', loaded);
but then in coffeescript as
loaded = ->
alert "hello"
document.addEventListener "DOMContentLoaded", loaded
it seems neither the event method is being fired as opposed to javascript version
Anyone could help me find out what is happening?
Thanks
UPDATE
if running the console after the page is loaded I can get the following to work without problem:
CoffeeScript.eval('document.write "<h1>testing</h1>"')
but still wondering why the page itself is not showing automatically
Works on Firefox and Chrome but not in Safari
It seems the page is not showing if using Safari 5.0.3
This is a humdinger, but after investigating, I've got your answer:
The way
coffee-script.js
works is that it looks for, and runs, scripts withtype="text/coffeescript"
after the document has loaded. In the case of Safari, that means thatis equivalent to
which silently fails. Note that making the insertion with the
document.createElement
method described by Erlend, or with a library like jQuery, will work fine.Since this works in Chrome, I'd go ahead and call it a Safari bug. But the real moral of the story is: Don't use document.write.