Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

1. Getting Started > In-Browser Compilation

In-Browser Compilation

When developing web applications, a time will come when you want to write some CoffeeScript directly inline in your HTML2 file. CoffeeScript does allow you to do this, and I will show you how. However, I want to caution you against doing such a thing. First, there is a very good reason why practices such as Unobtrusive JavaScript3 have become so popular recently. Although being able to execute CoffeeScript in the browser is nifty, it really is not the best compilation option available to us. By keeping your JavaScript in separate files and out of the HTML layer, you are able to keep your code cleaner and degrade more gracefully in environments that don’t support JavaScript.

At first, writing Unobtrusive JavaScript can be a bit confusing and difficult, but after a while it becomes easier to write, more reusable, and more logical to do so. Using tools like jQuery, you can wait for the page to load and attach all the appropriate JavaScript to the correct objects on the page. However, sometimes you do have to prime the pump, so to speak. Usually this means calling an init method, perhaps passing in some JSON4 to do so. I would encourage you to write this tiny amount of code using pure JavaScript. However, should you really want to write it in CoffeeScript, there is a way to let the browser compile it for you.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial