Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bootstrap Form 16 42
Reactjs with .NET 3 80
Any Way to rotate banner ads from Commission Junction? 2 38
Word Press Fixes to Google Speed Test Issues 2 26
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
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…
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…

730 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