LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-13-2010, 02:39 PM   #1
fruitwerks
Member
 
Registered: Apr 2009
Posts: 80

Rep: Reputation: 15
JavaScript: window.onbeforeunload


Hi all, I am using window.onbeforeunload to keep people from accidentally leaving. I was hoping to intercept this event, run my own dialog box and load another page that closes the window, or of course, cancel.

I'm needing this because my site is a single page and I can't figure out how long people are visiting for, if I could load an exit page, then I can figure this out.

Thanks!

Last edited by fruitwerks; 11-13-2010 at 07:58 PM. Reason: Solved / Alternate solution.
 
Old 11-13-2010, 02:43 PM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
If you're asking for a way to create your own custom dialog box then you can't. Your best bet is to run a confirm JS dialog when the user is leaving the page. From there based on the result you can either return false (don't follow) or have it location.href (go to another webpage).

It is a netscape security feature that JavaScript can't close a window with window.close() unless that window was opened by JavaScript with window.open.

I'd like to inform you that 99% of users will hate a feature that blocks them from leaving your site. I know I would (FYI).

edit: another method would be to simply load an exit page with window.onbeforeunload and in the exit page have a window.onload function which would throw a dialog box for the user to confirm that they wish to leave. You could use the history to go back or a form submission to go forward.

Last edited by sag47; 11-13-2010 at 03:02 PM.
 
Old 11-13-2010, 02:51 PM   #3
fruitwerks
Member
 
Registered: Apr 2009
Posts: 80

Original Poster
Rep: Reputation: 15
I was thinking that was going to be the answer. I had this without a function name previously, but here is what I have now. How would I do a confirm? And would the yes reply even be able to process anything further?

Code:
window.onbeforeunload = confirmExit;

function confirmExit() {
    return "Please use the Navigation menu - click OK to leave our site.";
};
The whole idea is to call an exit page and close it after it loads.

Thanks again!
 
Old 11-13-2010, 03:12 PM   #4
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
From what you want to do I don't think it can be easily done with clientside JavaScript. It is a security feature because you don't want browsers being hijacked with JS. I did find a little hack which prevents a user from leaving a webpage. But I doubt you'd have a use for it.

Code:
window.onbeforeunload=function(){window.setTimeout(function() { window.location.href=window.location.href; }, 10)}

Last edited by sag47; 11-13-2010 at 03:15 PM.
 
Old 11-13-2010, 03:18 PM   #5
fruitwerks
Member
 
Registered: Apr 2009
Posts: 80

Original Poster
Rep: Reputation: 15
yeah things are not looking good, cool script though, that would be a real pia for the end user!

Basically the issue is that I have no idea how long people are staying on my page, it's all ajax and js, and without an exit page I can't figure this out. I guess this is the classic flash website problem.

Thanks!
 
Old 11-13-2010, 07:57 PM   #6
fruitwerks
Member
 
Registered: Apr 2009
Posts: 80

Original Poster
Rep: Reputation: 15
I decided to research how flash sites handle GA, well I wasn't horribly surprised, but I found some nice features that would work for me. In all my 'page loads' (ajax calls) I call the same function to handle everything, so I came up with this snippet...

Code:
function gaPush(a,b,c) {
_gaq.push(['_trackEvent', a, b, c]);
return;
}
window.onbeforeunload = function () {
gaPush('Nav Attempt','possible exit');
   return "Please use the Navigation menu - click OK to leave our site.";
}
This way I can see what area are being viewed, and when they try to leave. I can't confirm this works perfectly, but if not I am very close, I'll have to wait for the GA results.

Thanks All!
 
Old 11-14-2010, 12:56 AM   #7
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Why don't you just have Ajax links load the content and any links that lead off-site simply call them with a target="_blank".

That's what I do on my website.

I'm curious, why do you want to know how long people are at your site?

If you're still intent on using JavaScript you could do something like the following:

Code:
var arrive=new Date();
var atime=arrive.toString();
atime=atime.split(' ');
atime=atime[4];
atime=atime.split(':');
atime=eval(atime[0])*3600+eval(atime[1])*60+eval(atime[2]);

function leaveTime()
{
	var leave=new Date();
	var ltime=leave.toString();
	ltime=ltime.split(' ');
	ltime=ltime[4];
	ltime=ltime.split(':');
	ltime=eval(ltime[0])*3600+eval(ltime[1])*60+eval(ltime[2]);
	
	var time_spent=ltime-atime;
	//account for 11pm to 1am
	if(time_spent<0)
	{
		time_spent=24*3600-atime+ltime;
	}
	var hrs=Math.floor(time_spent/3600);
	var mins=Math.floor((time_spent/3600-hrs)*60);
	var secs=Math.round(((time_spent/3600-hrs)*60-mins)*60);
	alert("You spent " + hrs + " hrs " + mins + " mins " + secs + " secs at my website!");
}
window.onbeforeunload=leaveTime;
Instead of having the amount of time they spent alerted in an alert box you could use AJAX to submit the information to your server. Then you can handle it however you want server side (writing to a text file or database or whatever).

Last edited by sag47; 11-14-2010 at 01:25 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for Textbrowser with JavaScript Support or Elink Javascript Support rohezal Linux - Software 4 09-01-2009 01:02 PM
Ubuntu Edgy/Firefox 2/Javascript - Firefox closes accessing websites with Javascript Interdictor Ubuntu 8 11-02-2006 11:58 AM
window.open() :- javascript Manashi Programming 7 05-03-2006 05:25 AM
Javascript - what to use rather than window.close() then window.open() davee Programming 2 04-11-2005 08:28 AM
Javascript: New Window Script trickykid Programming 1 03-22-2001 01:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration