?
Solved

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

Posted on 2009-04-14
5
Medium Priority
?
535 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:simonlai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
sam2912 earned 1500 total points
ID: 24141895
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
</script>

Open in new window

0
 

Expert Comment

by:ping2peng
ID: 24141994
hacking it out of the box i suggest cookies <a href="http://www.willmaster.com/library/javascript/detecting_a_back_button_click.php">here</a> is a sample hack

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




0
 

Expert Comment

by:ping2peng
ID: 24142068
btw what i mean use cookies if its a non-sensitive information, use session variables rather than cookies for sensitive information.
0
 
LVL 1

Author Comment

by:simonlai
ID: 24145843
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.

thanks.

0
 
LVL 14

Expert Comment

by:sam2912
ID: 24150430
The code I provided in my first post will prompt the user when they leave the page. You cannot hook the close window button.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question