Is is possible to load .coffee script file to brow

2019-03-05 04:46发布

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.

3条回答
一夜七次
2楼-- · 2019-03-05 05:03

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?

查看更多
戒情不戒烟
3楼-- · 2019-03-05 05:17

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.

查看更多
不美不萌又怎样
4楼-- · 2019-03-05 05:20

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.

查看更多
登录 后发表回答