Last active
August 29, 2015 14:22
-
-
Save mateeyow/eeefcb7f968776f09450 to your computer and use it in GitHub Desktop.
Send ajax request when someone leaves the page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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