I recieved a very helpful answer from Eric on how to display a confirmation is the user clicks on a browser back button, giving the user the choice if they want to go back or not.
Below was his answer:
You can use the beforeunload event to achieve this.
$(window).bind("beforeunload", function() {
return "HI"; // Return whatever message you want
});
Fiddle
(To use the fiddle, just reload the page or navigate away or whatnot.)
I would also suggest you unbind this upon someone leaving the page in the desired way (a cancel button?), or you may run into undesirable behavior.
For example:
$("a").click(function() {
$(window).unbind("beforeunload");
});
QUESTION
Now below is something we discussed and we don’t know if there is a clean way to do this but what I want to try and achieve is that if the user does confirm that they want to leave the page, then I want to navigate the user to a different script (Text4.php) and in this script it simply destroys the session and logs the user out. But I want to know if first of all if this is possible and second of all what is your ideas in order to answer this question?
You don’t need JavaScript to do this. I would do the following in your initial page.
When the page loads it looks if a variable is set, if so log out, if not make it.