CDN 1.4.0 以上的 CoffeeScript 无法调用 JavaScript 函数

CoffeeScript from CDN above 1.4.0 cannot call JavaScript function

链接 1.10.0 版 coffee-script.min.js 时,CoffeeScript 代码无法运行。

<!DOCTYPE html>
<html lang="en">
<head> <meta charset='utf-8'> </head>
<body>
<div id="myDiv">Content</div>
</body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js" > </script>
<!--
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/coffee-script/1.4.0/coffee-script.min.js" > </script>
-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/coffee-script/1.10.0/coffee-script.min.js" > </script>
<script type="text/coffeescript" >
$("#myDiv").text "New Content"
alert "Hello world"
</script>
</html>

最新的工作 CDN coffee-script.min.js是1.4.0。有什么问题?

我认为那些托管在 CDN 中的脚本不适用于浏览器。 我试过你的代码,正如你所说,它不起作用,并且有以下控制台日志。

Uncaught ReferenceError: require is not defined

require是commonJS的规范,浏览器默认没有实现。

如果您不想编译并让它轻松工作,可以使用下面的代码。

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset='utf-8'>
</head>

<body>
  <div id="myDiv">Content</div>
</body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js">
</script>
<script type="text/javascript" src="http://coffeescript.org/extras/coffee-script.js"></script>
<script type="text/coffeescript">
  $("#myDiv").text "New Content"
  alert "Hello world"
</script>

</html>

官网介绍了这种方式。 http://coffeescript.org/

While it's not recommended for serious use, CoffeeScripts may be included directly within the browser using tags. The source includes a compressed and minified version of the compiler (Download current version here, 39k when gzipped) as extras/coffee-script.js. Include this file on a page with inline CoffeeScript tags, and it will compile and evaluate them in order.

CDN 提供商打包错误,请参见此处:

https://github.com/jashkenas/coffeescript/issues/3811

https://github.com/cdnjs/cdnjs/issues/4869