我是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.jsAnd 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 meBut 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.
更多推荐
发布评论