4

I want to update database when user leave the site. I am using php for my site. I am using javascript function onbeforeunload and call php through ajax to update database.

Now the problem is that the function onbeforeunload is not supported by Opera and Safari, and so the entry should be deleted from database is still over their and affect my database and site.

Any idea or suggestion what i can do?

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
coder1087
  • 115
  • 1
  • 2
  • 9
  • You can't see `onbeforeunload` in the list provided by [W3Schools](http://www.w3schools.com/jsref/dom_obj_event.asp) Why you should use that event? What is the requirement to make it unsuitable to use `onunload` event which is in the list? – Saeed Neamati Jul 19 '11 at 10:21
  • Saeed, if i use onunload than i cant call php through ajax to update my database. – coder1087 Jul 21 '11 at 18:09
  • "You can't see onbeforeunload in the list provided by W3Schools Why you should use that event?" Is W3S your dad or something? There are plenty of legitimate user interactions that only onbeforeunload can perform. Gmail and facebook both use it.. whether it's in W3S' rolodex means little. – Alkanshel Aug 03 '11 at 18:37
  • `onbeforeunload` is now supported in Opera 15 based on Webkit. – wojo Jul 03 '13 at 16:06

4 Answers4

4

onbeforeunload seems to be a proprietary function from Microsoft. Firefox implemented it while Safari and Opera didn't.
http://www.designerstalk.com/forums/help-me/46310-onbeforeunload-safari-issues.html

Try the window.onunload event instead.
https://developer.mozilla.org/en/DOM/window.onunload

Example:

function unloadPage() {
   alert("unload event detected!");
}
window.onunload = unloadPage;
Mike
  • 2,567
  • 3
  • 23
  • 35
1

OnBeforeUnload is supported in Safari - what is not supported is the use of AJAX ... you need to use a Synchronous call - you can use alert to verify this.....

Manse
  • 37,765
  • 10
  • 83
  • 108
1

onbeforeunload should work in Safari. See Can I pop up a confirmation dialog when the user is closing the window in Safari/Chrome? (I just tested in Google Chrome and it works)

Community
  • 1
  • 1
Sparky
  • 4,769
  • 8
  • 37
  • 52
0

Beacon API (experimental technology) seems suitable for this kind of tasks.

PinguinoSod
  • 85
  • 1
  • 9