在html文件中调用coffeescript文件中的函数

Call function from coffeescript file in html file

本文关键字:文件 函数 coffeescript 调用 html      更新时间:2023-09-26

我是一个咖啡脚本新手。下面的文件main.js是在我包含我的coffeescript文件functions.coffee后生成的:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
require('../../modules/common/frontend/functions');

},{"../../modules/common/frontend/functions":2}],2:[function(require,module,exports){
//Link selection in menu
function setCurrentlink(){
    var currenturl=window.location.href;
    var part=currenturl.match(/'n'/displaywizard'/(.*)$/g);
    alert(part);
}
...
},{}]},{},[1])
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9icm93c2VyaWZ5L25vZGVfbW9kdWxlcy9icm93c2VyLXBhY2sv...

我一直试图调用函数setCurrentlink从html文件:

<script type="text/javascript">setCurrentlink();</script>

我试着把链接到js文件在头部部分,在页面的底部,但我得到一个错误说"找不到变量setCurrentlink"。请帮我找出我的错误或任何我遗漏的地方。

functions.coffee

#@export functions
#Link selection in menu
setCurrentlink = ->
  currenturl = window.location.href
  part = currenturl.match(/'n'/displaywizard'/(.*)$/g)
  alert currenturl
  return
...

您的函数是在函数内定义的,因此它的作用域仅限于该函数。您需要全局定义函数才能正常工作。也就是说,你可能用错了方法。如果我对你们的目标有更多的了解,我可以再给一些建议。