Skip to content

Instantly share code, notes, and snippets.

@mateeyow
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save mateeyow/eeefcb7f968776f09450 to your computer and use it in GitHub Desktop.

Select an option

Save mateeyow/eeefcb7f968776f09450 to your computer and use it in GitHub Desktop.
Send ajax request when someone leaves the page
(function () {
"use strict";
// Do something with the response data
// Since the visitor will leave the page we don't need this
/**
onResponse = function () {
if (httpRequest.readyState == 4) {
if (httpRequest.status === 200) {
// Do something with httpRequest.responseText
} else {
console.error('Error in sending ajax request');
}
}
}
**/
makeRequest = function (url) {
var httpRequest;
// Checks if browser is IE7+
if (window.XMLHttpRequest) {
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
httpRequest.open("GET", url);
httpRequest.send();
//httpRequest.onreadystatechange = onResponse;
};
window.onbeforeunload = makeRequest(enterUrl)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment