Solved

Fire function before close

Posted on 2008-10-29
1
2,293 Views
Last Modified: 2012-05-05
I'm trying to get some javascript to run before the user closes the form, and I'm using onbeforeunload, but it's firing on postbacks, in addition to when the form closes.  

Is there a way to get this script to only fire when the form is about to be closed.
0
Comment
Question by:ARACK04
1 Comment
 
LVL 16

Accepted Solution

by:
sh0e earned 500 total points
ID: 22837536
Not really.  You can check for window.event.clientY < 0.  This will still catch the X button.  

However, if you simply want to warn the user that they will lose data if they close the window, it may be better to use a flag that determines if there is data that has been changed.
Or, what you can do, is whenever you postback or navigate or anything else you don't want it to fire, you set a flag that prevents the confirmation from popping up.

<a href="http://google.com" onclick="flag=false;">link 1</a>
<form onsubmit="flag=false;">
<input type="submit"></input>
</form>
 
<script>
flag=true;
window.onbeforeunload = function(){if(flag)return "are you sure?";};
</script>

Open in new window

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

Title # Comments Views Activity
How to get CSS styled menu to maintain the open selected menu? 13 43
PHP: Filling Out/Creating a PDF 29 92
JavaScript code not working 3 31
Javascript Regex 7 20
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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)
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…

680 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