Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

840 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