在NodeJS中找不到的另一个.js文件中定义的函数(Function defined in another .js file not found in NodeJS)

编程入门 行业动态 更新时间:2024-10-09 20:26:12
在NodeJS中找不到的另一个.js文件中定义的函数(Function defined in another .js file not found in NodeJS)

我是Javascript和NodeJS的新手。

这是我网站的代码结构:

|source | | stylesheets | | | index.styl | | templates | | default.jade | | homepage.jade |static | | [.css generated] | |server.js |package.json |file.js

这是server.js :

var express = require('express') , logger = require('morgan') , app = express() , templateHome = require('jade').compileFile(__dirname + '/source/templates/homepage.jade') app.use(logger('dev')) app.use(express.static(__dirname + '/static')) app.get('/', function (req, res, next) { try { var html = templateHome({ title: 'Home' }) res.send(html) } catch (e) { next(e) } }) app.listen(process.env.PORT || 3000, function () { console.log('Listening on http://localhost:' + (process.env.PORT || 3000)) })

我的第二个.js文件包含一个简单的函数,一旦我的用户点击我在Jade定义的主页中的按钮就应该访问该函数:

block content button(onclick='downloadFile()') Click me

但似乎该函数未在当前范围中定义,即使它是在file.js定义的。 为了将我的其他.js文件考虑在内,我需要添加什么?

I am very new to Javascript and NodeJS.

Here is my website's code structure:

|source | | stylesheets | | | index.styl | | templates | | default.jade | | homepage.jade |static | | [.css generated] | |server.js |package.json |file.js

And here is server.js:

var express = require('express') , logger = require('morgan') , app = express() , templateHome = require('jade').compileFile(__dirname + '/source/templates/homepage.jade') app.use(logger('dev')) app.use(express.static(__dirname + '/static')) app.get('/', function (req, res, next) { try { var html = templateHome({ title: 'Home' }) res.send(html) } catch (e) { next(e) } }) app.listen(process.env.PORT || 3000, function () { console.log('Listening on http://localhost:' + (process.env.PORT || 3000)) })

My second .js file contains a simple function that should be accessed once my user clicks a button in my home page defined in Jade like such:

block content button(onclick='downloadFile()') Click me

But it seems that the function is not defined in the current scope, even though it is defined in file.js. What do I have to add in order to take my other .js files into account?

最满意答案

您需要使用<script>元素加载脚本。 使用src属性指定JavaScript文件的URL。 您需要为文件提供一个URL,这可能是通过将其移动到static/目录以及CSS旁边来完成的。

<script src="/file.js"></script>

请注意,在服务器上运行的程序和在浏览器中运行的程序是两个不同的程序,即使它们是用相同的编程语言编写的。 如果file.js包含预期在服务器上而不是在浏览器中运行的JS,那么您将需要采取另一种方法。 此问题解决了在服务器和客户端之间传递值的问题。


另请注意, onclick和其他内在事件属性具有令人讨厌的陷阱 ,通常被认为不是最佳实践 ,并且通常应该用JS的绑定处理程序替换。

You need to load the script using a <script> element. Specify the URL to the JavaScript file using the src attribute. You will need to give the file a URL, this is probably most simply done by moving it to the static/ directory, alongside the CSS.

<script src="/file.js"></script>

Do be aware that a program which runs on the server and a program which runs in the browser are two different programs, even if they are written in the same programming language. If file.js contains JS that is expected to run on the server and not in the browser then you will need to take another approach. This question addresses the issues in passing values between server and client.


Also note that onclick and other intrinsic event attributes have nasty gotchas, are generally considered less than best practice, and should generally be replaced by binding handlers with JS.

更多推荐

本文发布于:2023-08-07 12:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464307.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中找   函数   定义   文件   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!