Solved

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

Posted on 2009-04-14
5
534 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 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

728 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