I start like I usually do in javascript, so:
$(document).ready ->
but when I save I get a document is not defined. So far I haven't been able to find how to make it work.
Edit: by save, I meant I was using coffee -w. The error was due to me forgetting to use the -c option.
To solve the"ReferenceError: document is not defined" error, make sure to only use the document global variable on the browser. The variable relates to the Document Object Model, which represents a web page that is loaded in the browser and can't be used on the server side (e.g. in Node. js).
The most common reason for this error is because you're using Node. That is to say, you are trying to access the document object on the server, but the server does not have access to the document object because it lives on the browser.
You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file. In other cases, I've seen people use the attributes of type="coffeescript" and type="coffee" , so they might work for you as well. Save this answer.
CoffeeScript is compiled into JavaScript. I'm not sure what you're trying, but in your exact case your CoffeeScript isn't going to look a whole lot different than your JavaScript. Try:
$(document).ready -> alert 'blah'
If that doesn't work, just do a test like
alert document.title
if that doesn't work, I'm going to suggest that you're not running this code inside a browser ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With