Is there any way to use CoffeeScript in client side?
问题:
回答1:
There are two ways:
- Compile the CoffeeScript to JavaScript and deploy it as you would any JavaScript file, or
- Use
coffee-script.js
, which allows you to put<script type="text/coffeescript>
tags in your page.
The latter isn't recommended for production use, but it's nice for development. See the related question: Is there a way to send CoffeeScript to the client's browser and have it compiled to JavaScript *there*?
回答2:
See also Webmake plugin for CoffeeScript -> https://github.com/medikoo/webmake-coffee
It allows you to organize coffee modules in Node.js style and bundle it for browser. It provides source maps support, so you can debug CoffeeScript files as they are, directly in a browser.
回答3:
To not compile everytime you can use -w param and coffee will compile the file everytime file change
coffee -wco src/ public/js
回答4:
Yes, it can be done by adding a CoffeeScript src
tag to the head section of your html page.
Download the CoffeeScript source from this path: http://coffeescript.org/extras/coffee-script.js
Copy and paste the below code and try to run in a browser:
<html>
<head>
<script type="text/javascript">
function printHelloJava(){
alert("Hello Javascript");
}
</script>
<script src="coffee-script.js"></script>
<script type="text/coffeescript">
@printHello = ->
alert "Hello Coffee Script"
</script>
</head>
<body>
<h1>Coffee Script on client side</h1>
<input type="button" onclick="printHelloJava();" value="Hello Java">
<br>
<input type="button" onclick="printHello()" value="Hello Coffee">
</body>
</html>
回答5:
You can also use CDN coffeescript for better and faster performance.
<script src="http://cdnjs.cloudflare.com/ajax/libs/coffee-script/1.7.1/coffee-script.min.js"></script>
or
<script src="https://cdn.rawgit.com/jashkenas/coffeescript/1.11.1/extras/coffee-script.js"></script>
Then use type="text/coffeescript"
for compile Coffee Script
.
<script type="text/coffeescript">
// add code here
</script>