call a function when user click "Back Button" and "X" button

Dear All:

isn't possible to call a javascript function when user click on "Back Button" on browser and reset html form?

any idea how to detect user click on "X" button on the right top browse or close the browserr?

current i would able to prevent user by adding this in body tag, onLoad="history.go(+1)". but not able to detect user click back button.

thanks in advance.
Who is Participating?
sam2912Connect With a Mentor Commented:
I don't think you can really stop a user from closing the window, exiting the browser, or killing the process just to prevent the user from leaving a page.

However, you can use the following code provided to prompt the user if they want to actually do so.
<script type="text/javascript">
window.onbeforeunload=function(){ return false; }; // Uses default exit confirm screen, all browsers

Open in new window

hacking it out of the box i suggest cookies <a href="">here</a> is a sample hack

if your doing ajax hmm.... i guess cookies would be good, iframe and anchors (#)

btw what i mean use cookies if its a non-sensitive information, use session variables rather than cookies for sensitive information.
simonlaiAuthor Commented:
dear sam2912:

i don't want stop the user to close the browser and i know it was impossible. i just want to know when user click on "X". i try out some web system, it able to prompt me a msg when i click on "X" only.


The code I provided in my first post will prompt the user when they leave the page. You cannot hook the close window button.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.