Is is possible to load .coffee script file to browser and execute?

落爺英雄遲暮 提交于 2020-01-11 13:33:09

问题


I am trying to load coffee script inside a sample.coffee file along with the coffee-script.js file and perform some simple operations on the HTML. But i am not able to load the sample.coffee file.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title></title>
    <script src="sample.coffee" type="text/coffeescript"></script>      
    <script src="coffee-script.js" type="text/javascript"></script>
  </head>
  <body>
  </body>
</html> 

Here is the coffeescript code inside sample.coffee

message = "Welcome to Coffeescript"
alert message

I referred the blog http://forgivingworm.wordpress.com/2010/09/27/running-coffeescript-in-browser/

I trying to run this in MVC4 project on .cshtml. So I am running under MS world.

I am not sure if this is possible or not? any insight into this would be greatly appreciated.


回答1:


I dont think this will work like you tried it. You will need something like requirejs and the coffeescript loader at https://github.com/jrburke/require-cs.

However I would strongly discourage this and rather have it compiled before loading into the browser (require.js is encouraged though). You might find grunt.js helpful for "building" your app before deploying.




回答2:


I am able to solve this issue by using the CoffeeSharp http://tomlokhorst.github.com/CoffeeSharp/ It gives handler for .coffee files also converts the coffeescript to javascript on the browser.

Also I am able to compile the coffeescript to javascript during the build event itself which solves deployment and performance related issues.




回答3:


That html works fine for me. I used the coffee-script.js found here: https://cdn.rawgit.com/jashkenas/coffeescript/1.11.1/extras/coffee-script.js and my sample.coffee is:

x = ->
  alert("hi")

x()

If you open the Javascript console, do you see any errors?



来源:https://stackoverflow.com/questions/15742415/is-is-possible-to-load-coffee-script-file-to-browser-and-execute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!