One question:
How do i differentiate when a page is refreshed event from a page/browser closed event?
Because it seems that the onbeforeunload event is triggered when a page is refreshed and when it's closed, and i didnt want that to happen ?

Edited by diafol: This has nothing to do with php any more

6 Years
Discussion Span
Last Post by diafol

I don't think it's possible to distinguish between these events. WRT refresh are you talking about the browser button / F5? If so, I can't see how you'd trap this event. If the refresh is via webpage link, you probably could stop the onbeforeunload event before completing the page refresh.

Don't take my view as the gospel truth though.


Here's some code I slapped together to show how to prevent the onbeforeunload:

<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<a href="crap.php" class="goodLink">BUG OUT FOR REAL</a><br />
<a href="crap2.php" >ASK ME</a><br />
<a href="crap3.php" class="goodLink">DEFINITELY BUG OUT</a><br />

<form method="post">
    <button class="goodLink">BUTTON: DON'T CHALLENGE ME</button><br />
    <button>BUTTON: OK ASK ME NICELY</button><br />
    <input type="submit" class="goodLink" value="SUBMIT: DON'T CHALLENGE ME" /><br />
    <input type="submit" value="SUBMIT: ASK ME NICELY :)" />    

<script language="JavaScript">
var popup = true; //default action is to popup
window.onbeforeunload = function(e){
    if(popup == true)return "Wanna leave?";

I have no idea whether this helps at all?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.