Solved

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

Posted on 2009-04-14
5
529 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
  • 2
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
sam2912 earned 500 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A Change in PHP Behavior with Session Write Short Circuit (http://php.net/manual/en/book.session.php#116217) (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now