33

In our rails rfq.js.coffee, we only have a simple js code:

$(function() {
  $('#need_report').change(function(){
    if ($(this).val() == true) {
      $('#report_language').hide();
    }  // end if
  });  // end change()
});  // end ready(function)

However this code causes an error saying that function() in first line is a reserved word. Since the first line is basically a jquery $(document).ready(function () {}), we have no clue why this error shows up. Any thoughts about it? Thanks so much.

2
  • 2
    Wait, is this CoffeeScript? Why are you using normal JS in CoffeeScript? Commented Jan 25, 2012 at 22:11
  • 7
    js2coffee.org will be your friend Commented Jan 25, 2012 at 22:21

3 Answers 3

48

You can't use standard JS like that in a Coffeescript file. Either rename the file to rfq.js, or convert it to coffeescript:

$ ->
  $('#need_report').change ->
    if $(this).val()
      $('#report_language').hide()
Sign up to request clarification or add additional context in comments.

4 Comments

Just renamed the file to js file. Thought js can be used in coffee script without any change. Also found a site to convert the js to coffee script or vise verse. js2coffee.org thanks.
Yeah, it's not like Sass vs CSS where you can use standard CSS inside an scss file.
However, after renaming the rfqs.js.coffee to rfqs.js, there is a strange error saying illegal character on line 9574 (There is only about 100 lines code in application.js) in application.js. I renamed the file name back to .coffee and the error disappeared. Strange enough!
I don't understand. According to the CoffeeScript site: "You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa)."
8

You can embed regular javascript by surrounding the code with back-ticks "`". I wish it worked like the other parsing languages as well...it took me lot's of unnecessary debugging and searching to figure that out. http://coffeescript.org/#embedded

Comments

2

Maybe you wrote JavaScript code into file with extension .coffee you can use js2.coffee to convert your code from JavaScript to CoffeeSecript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.