Ive written some code Im trying to use to tack a visitors ip, and how long they are on site for. Code:
<script>
var startTime = new Date();
window.onbeforeunload = $(function() {
/* var ip = (window.location != window.parent.location) ? document.referrer: document.location; */
/* var ip = "192.168.1.1"; */
var endTime = new Date(); //Get the current time.
var timeSpent = (endTime - startTime); //Find out how long it's been.
var ip = (window.location != window.parent.location) ? document.referrer: document.location;
$(window).load(function(event) {
$.post('ajax.php', {ip: ip, timeSpent: timeSpent});
});
});
</script>
What I don't get, is why this does not wait till a user tries to leave the site before running this script.
Can anyone help me get this to wait until then to run? Thank you!
Ive written some code Im trying to use to tack a visitors ip, and how long they are on site for. Code:
<script>
var startTime = new Date();
window.onbeforeunload = $(function() {
/* var ip = (window.location != window.parent.location) ? document.referrer: document.location; */
/* var ip = "192.168.1.1"; */
var endTime = new Date(); //Get the current time.
var timeSpent = (endTime - startTime); //Find out how long it's been.
var ip = (window.location != window.parent.location) ? document.referrer: document.location;
$(window).load(function(event) {
$.post('ajax.php', {ip: ip, timeSpent: timeSpent});
});
});
</script>
What I don't get, is why this does not wait till a user tries to leave the site before running this script.
Can anyone help me get this to wait until then to run? Thank you!
Share Improve this question edited Mar 12, 2013 at 20:51 jcubic 66.8k58 gold badges249 silver badges455 bronze badges asked Mar 12, 2013 at 20:46 user1789437user1789437 4681 gold badge8 silver badges22 bronze badges 6-
1
You are setting
window.onbeforeunload
to a jQuery object, not a function. – gen_Eric Commented Mar 12, 2013 at 20:48 - Can this be modified to work? – user1789437 Commented Mar 12, 2013 at 20:49
-
2
Why are you using
$(window).load(
inside there? I'd assume that when a user was leaving the page, the DOM would be ready. – gen_Eric Commented Mar 12, 2013 at 20:50 - This is one of those questions where it's like, man, you really don't know what this code is doing, do you? – Evan Davis Commented Mar 12, 2013 at 20:51
- Im trying to get the time on site. First by getting the start time, then, when a user leaves, grab the new time, the url of the parent window (this is iframed on 100+ sites), and use ajax to post to a php page and run more php with out a user prompt. – user1789437 Commented Mar 12, 2013 at 20:51
2 Answers
Reset to default 6There are a few issues with this code that I can see.
First off, $(function(){})
is short-hand for $(document).ready(function(){})
. This means it will run the function when the DOM is ready, and then return you a jQuery object ($(document)
).
Second, $(window).load(function(){})
isn't needed. I'd assume that when a user was leaving the page, the DOM would've already been loaded.
Third, window.onbeforeunload
(and window.onunload
) will not wait for your AJAX call to finish. You can try to use async:false
to make it wait (that may not work in all browsers).
$.ajax({
url: 'ajax.php',
data: {ip: ip, timeSpent: timeSpent},
async: false
});
(NOTE: window.onbeforeunload
doesn't work in all browsers; I know Opera doesn't fire it.)
Also, window.onbeforeunload
is used to ask the user if they want to leave the page or not. If you return a string from the event that will be presented to the user (except in Firefox).
If you want to send an AJAX call when the user leaves the page, I suggest using window.onunload
instead.
(function(){ // Anonymous function so startTime isn't global
var startTime = new Date();
window.onunload = function() { // set to a function
var endTime = new Date(); //Get the current time.
var timeSpent = (endTime - startTime); //Find out how long it's been.
var ip = (window.location != window.parent.location) ? document.referrer: document.location;
$.ajax({
url: 'ajax.php',
data: {ip: ip, timeSpent: timeSpent},
async: false
});
};
}());
You made a nice mess from this whole thing. All you need is:
var startTime = new Date();
window.onbeforeunload = function() {
var endTime = new Date(); //Get the current time.
var timeSpent = (endTime - startTime); //Find out how long it's been.
var ip = (window.location != window.parent.location) ? document.referrer: document.location;
$.post('ajax.php', {ip: ip, timeSpent: timeSpent});
};