【发布时间】:2017-06-27 00:41:01
【问题描述】:
我有一个 HTML 页面,当按下“Reset”按钮时,我总是会加载特定页面 (control.html)。
我不想拥有Reset 按钮,而是在浏览器刷新时执行相同的操作。
我该怎么做?似乎没有在所有浏览器中通用的浏览器刷新键码。 this post 也没有帮助。
我正在使用 nodejs 服务器。服务器代码如下:
var http = require('http');
var fs = require('fs');
var url = require("url");
var path = require("path");
var ent = require('ent');
//below creates the server functionality.
//You shouldn't need to change this.
var server = http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname
, filename = path.join(process.cwd(), uri);
var contentTypesByExtension = {
'.html': "text/html",
'.css': "text/css",
'.js': "text/javascript",
'.jpg': "image/jpeg",
'.png': "image/png"
};
//deal with post requests:
if (request.method == 'POST') {
var body = '';
request.on('data', function (data) {
body += data;
// Too much POST data, kill the connection!
if (body.length > 1e6)
request.connection.destroy();
});
request.on('end', function () {
try {
var post = JSON.parse(body);
deal_with_post_data(request,post);
response.writeHead(200, {"Content-Type": "text/plain"});
response.end();
return;
}catch (err){
response.writeHead(500, {"Content-Type": "text/plain"});
response.write("Bad Post Data. Is your data a proper JSON?\n");
response.end();
return;
}
});
}
//grab out REST requests and handle those, otherwise work as a normal file server.
else if(uri=='/rest'){
try {
rest_request(url.parse(request.url,true).query);
response.writeHead(200, {"Content-Type": "text/plain"});
response.end();
return;
}catch (err){
response.writeHead(500, {"Content-Type": "text/plain"});
response.write("Bad REST request.\n");
response.end();
return;
}
}else{
//return an error if a page is requested that does not exist.
fs.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
//by default, if no page is requested, serve index.html
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
//read the requested file and serve it up.
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
var headers = {};
var contentType = contentTypesByExtension[path.extname(filename)];
if (contentType) headers["Content-Type"] = contentType;
response.writeHead(200, headers);
response.write(file, "binary");
response.end();
});
});
}
});
// Loading socket.io
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
// When a "message" is received (click on the button), it's logged in the console
socket.on('message', function (message) {
console.log(' Received:' + message);
socket.broadcast.emit('C', message);
});
socket.on('wrong', function (message) {
console.log(' Received:' + message);
//signal all the clients there were some malicious attempts
socket.broadcast.emit('W', message);
});
//messages related to GT
socket.on('messageGT', function (message) {
console.log(' Received:' + message);
socket.broadcast.emit('mGT', message);
});
socket.on('wrongGT', function (message) {
console.log(' Received:' + message);
//signal all the clients there were some malicious attempts in GT
socket.broadcast.emit('wGT', message);
});
});
server.listen(8080);
【问题讨论】:
-
重置按钮按下事件的代码在哪里?你可以在javascript中使用相同的。我不明白为什么需要在 nodejs 中完成。看看这个链接,他们使用 JS stackoverflow.com/questions/5411538/redirect-from-an-html-page 重定向到加载页面
标签: javascript jquery html node.js