如何在浏览器刷新时加载特定页面

时间:2017-06-27 00:41:02

标签: javascript jquery html node.js

我有一个HTML页面,我总是在" control.html"时加载特定页面Reset)。按下按钮。

我不想拥有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);

1 个答案:

答案 0 :(得分:1)

刷新基本上是页面加载 所以,如果你需要页面加载处理程序:

document.addEventListener('DOMContentLoaded', function(e) {
  // your reset button handler here
});

或者,如果您需要页面休假:

document.addEventListener('onbeforeunload', function(e) {
  // your reset button handler here
});