Home » Javascript » Article
|
|
| Viewed: 57526 times |
Rating (24 votes): |
|
3.1 out of 5 |
|
|
|
Javascript onUnload Event Handler
Live Demo
The onUnload Event Handler allows you to perform an action as the user leaves the page. Click here to see an example of an javascript alert that pops up when leave the page.
How did we do that? Simple : within the body tag of the loading page, put the following:
<BODY onUnload="alert('Are you sure
you have spent long enough on this lovely page?')">
| |
|
As usual, keep all the above on one line to ensure it works properly. Two things to watch out for here : remember that Javascript is case sensitive, and will not work if you use 'onload' instead of 'onUnload'. Secondly, don't confuse your single and your double quotes!
|
|
View highlighted Comments
User Comments on 'Javascript onUnload Event Handler'
|
Posted by :
Archive Import (Mikey) at 01:34 on Tuesday, April 15, 2003
|
Doesn't seem to work with IE5.5 - onLoad and so forth work correctly, but not onUnload. Pity.
| |
Posted by :
Archive Import (Tadus) at 18:32 on Wednesday, June 18, 2003
|
It's work on IE5.5 and even IE 5. It's work also on my IE 6.
| |
Posted by :
Archive Import (Joe) at 00:03 on Thursday, June 26, 2003
|
The onUnload event is handled when you refresh the page as well. Hence it doesn't solve the problem if you want to execute a piece of code for when the user is exiting the page. Does anyone know how to perform an action when leaving the page only (e.g. to logout someone)?
| |
Posted by :
Archive Import (M Suresh) at 10:52 on Sunday, June 29, 2003
|
Hi,
I would like to display a message when the user clicks on the "X" button of the browser. for that, I have used onUnload event and in that I am displaying an alert message. This works fine if I click "X". But, even if I refresh the page or if I click any action on the page, same onUnload method is fired. How to restrict my action only to "X" button click?
Thank you,
Suresh M
| |
Posted by :
PlasticMagnet at 02:33 on Saturday, July 17, 2004
|
I have IE 6 and Mozilla Firefox 0.8 and this didn't seem to work at first.. until I looked at the Java console errors and saw that my string was unterminated... Once i made sure sure everything was on one line, it worked fine.
| |
Posted by :
samit at 12:18 on Thursday, September 30, 2004
|
Hi,
I am trying to post a comment but i am not able to find a place so i am doing it over here. Sorry for any inconvenience......
I am trying to design a web page and it has a kind of slide show tool that uses javascript. Now if the javasript is disabled i want that i show only the background .
SO my question is that is there a way to check in the code if whether java script is enabled for that particulat browser and act accordingly.
My aim is something like this:
if (java sript enabled)
{
// do something
}
else
// do something
Please let me know if some one has a solution or email me to samitkumbhani@gmail.com
thank you
samit
| |
Posted by :
raj24215565 at 04:33 on Sunday, August 14, 2005
|
Hi,
I would like to display a message when the user clicks on the "X" button(close) of the browser. for that, I have used onUnload event and in that I am displaying an alert message. This works fine if I click "X". But, even if I refresh the page or if I click any link on the page, same onUnload method is fired.
How to restrict my action only to "X" button click?Plz suggest
Thank you,
| |
|
To post comments you need to become a member. If you are already a member, please log in .
| RELATED ARTICLES |
Javascript Onload Event by Jeff Anderson
Sometimes you need to perform an action immediatley after the page has loaded. That's when the onLoad Event Handler comes in handy |
 |
Javascript - Enable and Disable form elements by Jeff Anderson
This is a relatively little known and under-used feature of javascript which can be very useful in guiding a user through a form. Using the disabled tag, you can switch on and off elements in a form. |
 |
Form Validation Function by Jeff Anderson
A javascript validation function that you can use to validate all types of forms. |
 |
Check IsNumeric Function by Jeff Anderson
A javascript validation function to check whether the details entered by a user are numeric. |
 |
JavaScript Field Is Empty Form Validation by Jeff Anderson
This javascript function allows you to check whether a form field has been completed or not. |
 |
Check Email Validation Function by Jeff Anderson
A javascript validation function to check whether the user has entered a valid email address in a form. |
 |
Multiple submit buttons on a single form by Kiran Pai
This script shows you how to submit the contents of a form to different programs depending on which Submit button you press. Additionally it also shows how to call two different functions when you press the Submit button. |
 |
Validate Form and Disable Submit Button by Marylou Marks
I have a form that validates info, but I also want to disable the submit button. The disable part worked before adding the form validating. |
 |
Simple date validation by Chris Hogben
function that takes a date in three parts, day, month and year - returns true if it's a valid date. |
 |
Multiple submit buttons with form validation by Paul Eckert
This code shows how to redirect a user to multiple sites, depending on which submit button he presses, but only after the form validates correctly. |
 |
| |