Skip to content

Instantly share code, notes, and snippets.

@donabrams
Created March 23, 2012 16:00
Show Gist options
  • Select an option

  • Save donabrams/2172114 to your computer and use it in GitHub Desktop.

Select an option

Save donabrams/2172114 to your computer and use it in GitHub Desktop.
onbeforeunload testing
<!doctype html>
<html>
<head>
<title>onbeforeunload ajax test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(function() {
window.onbeforeunload = function() {
console.log("saving");
$.ajax("saved", {async:false});
};
$.ajax("tried").then(function() {
console.log("redirecting");
window.location = "list";
}, function() {
console.log("general ajax error");
console.dir(arguments);
});
console.log("setup complete");
});
</script>
</head>
<body>
Still processing the ajax requests
</body>
</html>
<!doctype html>
<html>
<head>
<title>onbeforeunload ajax results</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(function() {
var a = {tried :[], saved: []}; //TODO: get this from ajax
var supported = $("#supported");
var unsupported = $("#unsupported");
var listed = [];
$.each(a.tried, function(i, val) {
if (listed.indexOf(val) < 0) {
listed.push(val);
if(a.saved.indexOf(val) >= 0) {
supported.append("<li>" + val + "</li>");
}
else {
unsupported.append("<li>" + val + "</li>");
}
}
});
});
</script>
</head>
<body>
<h2>Unsupported</h2>
<ul id="unsupported"></ul>
<h2>Supported</h2>
<ul id="supported"></ul>
</body>
</html>
var express = require('express');
var fs = require('fs');
var host = 'HOST HERE';
var serverConfig = {
key: fs.readFileSync('localServer.key'),
cert: fs.readFileSync('localServer.crt')
};
port = 5000;
app = express.createServer(serverConfig);
app.configure(function() {
app.use(express.cookieParser());
app.use(express.session({ secret: "keyboard kitty" }));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
var tried = [];
var saved = [];
app.get('/', function(req, res) {
res.sendfile('onbeforeunload.html');
});
app.get('/tried/?', function(req, res) {
tried.push(req.headers['user-agent']);
res.send(200);
});
app.get('/saved/?', function(req, res) {
saved.push(req.headers['user-agent']);
res.send(200);
});
app.get('/list/?', function(req, res) {
res.send(JSON.stringify({tried:tried, saved:saved}));
});
app.listen(port, host);
console.log("Express server listening on host %s port %d in %s mode", host, port, app.settings.env);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment