Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6952907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:24:36+00:00 2026-05-27T14:24:36+00:00

I’m now making the transition towards writing all my javascript code using Coffeescript, But

  • 0

I’m now making the transition towards writing all my javascript code using Coffeescript, But I’m frustrated because the simplest of examples is causing me problems. As of now, I’ve done more than an hour of research without being able to find the answer to this…

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js" type="text/javascript" charset="utf-8"></script>
  <link href="sheet.css" rel="stylesheet" type="text/css" media="screen" />
  <script type="text/coffeescript">
    $ ->
      sayHi()

    sayHi = ->
      alert 'Hi there!'
  </script>
</head>    
<body>
  <div id="all">  
  </div>  
</body>
</html>

As it is clear from the code above, I’m just trying to make the call to the sayHi() function work from inside the jQuery’s ready handler. But the error I’m getting is the following:

Uncaught TypeError: undefined is not a function

Please help me, According to the compiler and tutorials I’ve read this ‘should‘ work, But I don’t know what I’m doing Horribly wrong for this to not run 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:24:36+00:00Added an answer on May 27, 2026 at 2:24 pm

    text/coffeescript tags have a key difference from text/javascript tags. They dont “run” until the document loads. This is because the coffee script library has to find all the coffee script tags and compile them, and it has to wait until the DOM ready so it can be sure to find them all.

    The other issue is that jQuery will fire the DOM ready callback immediately if the event already happened. And in this case it has.

    So when this is compiled to JS you get this:

    var sayHi;
    $(function() {
      return sayHi();
    });
    sayHi = function() {
      return alert('Hi there!');
    };
    

    So what happens is:

    • declare the sayHi variable with no value, making it undefined.
    • Create the DOM ready callback for jQuery that uses this variable.
    • jQuery runs the callback function immediately because DOM ready has already happened.
    • The callback function executes, and tries to run sayHi() which is still undefined.
    • After the callback runs, sayHi is then set to the function you wanted to run.

    Now if this was a normal JS tag, it could have run before the document loaded, and then it would have worked fine because by time the callback actually ran, then sayHi would have been assigned properly.

    To fix it you should assign the function BEFORE you run pass in the callback. Or you can skip doing the $(->) entirely since you know DOM ready fired already. But really, this is one major reason you really shouldn’t use coffeescript tags. It’s really not the same as using a JS tag. And one of many reason this is not the recommended approach for using CoffeeScript on a real website.

    So compile your coffee script before your browser sees it like a responsible developer 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a jquery bug and I've been looking for hours now, I can't
I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.