# Detecting Browser Back/Fwd Button Click



## sudoku-san (Dec 11, 2011)

I see there are lots of window events now available, but don't find one related to the browser Back and Forward buttons.

Is there a way to detect this and, using JavaScript, dismiss the action if necessary? I assume onbeforeunload will fire, but how to distinguish Back/Fwd button as the cause?

Thanks


----------



## jamiemac2005 (Jul 5, 2007)

Hi, unfortunately giving a script control or even knowledge of where the page is going when the user leaves poses a security and privacy risk so this is not possible. Whilst you can make the browser go back, you can't find out what the next entry in history.back would be, or history.forward and unfortunately the script can only "know" that the user is leaving the page, not where they're going or how they're getting there.

This page goes over pretty much what you can do with javascript to deal with the user leaving the page but it seems you have a pretty good idea of what that is already. And in most modern browsers (i know firefox for sure) the onbeforeunload interface has changed somewhat to put the user in control (Good for when you end up on a page you don't like, bad for the legitimate scripters out there)

What is it exactly that you're trying to do? And what is the situation (What page would you be going back/forward to)... There may be a way to do something similar but it depends on exactly what you're attempting to do.

Cheers,
Jamey


----------



## sudoku-san (Dec 11, 2011)

Jamey,
Thanks for the informative reference.
What I'm trying to do -- using ASP.NET and JS --
1) present a puzzle to user to be solved using JavaScript tools. The solving process involves several tools, none of which require a postback. Thus, after puzzle is started, user may lose web connection indefinately without loss of functionality.
2) when the user decides to close the puzzle using a tool panel button, I check if the web connection is still alive. If not, I so advise the user and refrain from posting back -- very smooth.
3) problems arise when user clicks refresh or back buttons. I find that the window.onbeforeunload event doesn't fire in this case, since we are unloading and reloading the same document. However, when these buttons are clicked, an "onbeforeunload" event set as HTML attribute in the <body..> element will fire, but if I determine that the web is down, returning "false" from the eventhandler does not stop the unload. Best I can seem to do is alert user that something bad is going to happen.
4) using onunload instead of onbeforeunload has similar problem -- can't abort the unload. 
Guess maybe I'm stuck with telling user not to use these browser buttons unless he is sure the web connection is up.
Any further suggestions?
Thanks again.


----------



## jamiemac2005 (Jul 5, 2007)

Hmmm, that in itself is very interesting. It's nice to see someone coding with a wide range of eventualities in mind.

Okay, I remember having a few issues in the past with events in the body not passing on the full event object to javascript. I forget the exact method I used to fix it, so I'll have to check my methods and see if I can work out a way to do that.

So in 2 above, is the web connection check done on the client side? in javascript?
If you don't mind posting that code &/ the method call and event handlers I could get a greater understanding of why it's not working on back/refresh. I'm certain there will be a comfortable work around or fix for the problem and I'd assume that the problem lies in the method calls themselves because of previous issues I've encountered but that depends entirely on how you're checking the web connection?

Yeah my suggestion for now would be to just advise the user that closing the page if your web-check fails is not advisable. But as I said, if you don't mind posting the relevant code (the bits that are failing) then I might have more of an idea for a proper fix for the issue?

Cheers,
Jamey


----------



## sudoku-san (Dec 11, 2011)

Jamey,

Thanks again for considering my problem.

Regarding your question, when user closes the puzzle I want to assure not only that internet is alive but also that my website is alive, so I planted a text file ("online.txt") on my website and try to read it using an XmlHttpRequest when user clicks on the "close puzzle" button. A query string containing current time info is used to bust the browser cache, assuring that the "online.txt" file is actually being read from the site.

This is all done in javascript, and a false return prevents postback.

---------------------

function chkNet() {
var amon = false;
myurl = String(location.href);
hx = myurl.indexOf("#");
if (hx != -1) {
myurl = myurl.substr(0, hx - 1);
}
txturl = myurl.replace("CloudPuzzle.aspx", "online.txt");
nwd = new Date();
qadd = nwd.getTime();
txturl = txturl + "?" + qadd;
amon = gettxtdata(txturl); //returns True if my website is alive 
return amon;
}

function gettxtdata(url) { //
getXmlHttpRequestObject();
if (xmlHttp) {
xmlHttp.open("GET", url, false); //synchronous call
xmlHttp.send(null);
if (xmlHttp.status == 200) {
return true;
}
else {
return false;
}
}
}


----------

