Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use CoffeeScript with JQuery, I get a "document is not defined" error

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.

like image 817
Phrodo_00 Avatar asked Feb 02 '11 21:02

Phrodo_00


People also ask

How do I fix document not defined error?

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).

Why is it showing document is not defined JavaScript?

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.

How do I use CoffeeScript in HTML?

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.


1 Answers

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 ;)

like image 89
kelloti Avatar answered Oct 11 '22 00:10

kelloti