View Full Version : can an event handler cancel another event handler?
mr_evans2u
03-26-2003, 08:15 PM
I am using -onbeforeunload=>"closeIt()", to warn my users of leaving the page before saving their information. This works great, but it also warns when clicking the submit button. Is there a way with maybe onclick or something else to cancel out this event if the submit button has been clicked?
liorean
03-26-2003, 08:35 PM
delete window.onbeforeunload;
// Should work.
Or you can have a variable set on submit, and only execute the onbeforeunload if that isn't set. But I think delete is the best alternative.
Roy Sinclair
03-26-2003, 08:57 PM
Use:
window.onbeforeunload = null;
null is the value it had before you assigned a function object to it so setting it back to null will disconnect the function from it.
mr_evans2u
03-26-2003, 09:18 PM
where do I put the code?
Roy Sinclair
03-26-2003, 09:22 PM
You put the code into your submit button:
<input type="submit" ... onsubmit="window.onbeforeunload=null;return true;">
mr_evans2u
03-26-2003, 09:43 PM
It didn't affect the unbeforeunload at all. Still when clicking the submit button I get the dialog box. I tried onsubmit and onclick with the same results.
liorean
03-26-2003, 09:52 PM
Well, show us your code - we might be able to see what you've done wrong from the code in a way we never would from just your explanations of it.
mr_evans2u
03-26-2003, 10:01 PM
###in .js file####
function closeIt()
{
event.returnValue ="____________________________________________\n\n"+
"Closing before submittingwill cause loss of all data!\n\n"
}
######part of the html##########
print start_html(-title=>"Sieve Filter Administration",
-script=>{-language=>'javascript',
-src=>'load.js'},
-onbeforeunload=>"closeIt()",
-background=> "gray.jpg"),
submit(-name=>"Save_Send",
-value=>"Submit Filter Modifications",
-onClick=>"return checkAllItems(this.form)",
-ondblclick=>"JavaScript:alert('Do not double click')",
-onsubmit=>"window.onbeforeunload = null;return true;")));
liorean
03-26-2003, 10:12 PM
Ah - the onsubmit goes on the form tag, not the submit button. That's your problem in getting it to work. I suggest you merge a few of those things into this:
<form onsubmit="return checkAllItems(this);" [...]>
and in the checkAllItems function:
function checkAllItems(form){
result=true;
/*checking all elements -
set result to false if anything is wrong */
if(result)delete window.onbeforeunload;
return result;
}
Then you can remove the onclick and onsubmit attributes on the submit button.
Roy Sinclair
03-26-2003, 10:29 PM
Originally posted by liorean
Ah - the onsubmit goes on the form tag, not the submit button. That's your problem in getting it to work. I suggest you merge a few of those things into this:
My bad! I started to do it as an onclick and thought I should use onsubmit instead but didn't change the tag.
mr_evans2u
03-26-2003, 10:51 PM
I guess I'm just brain dead today! I can't get it to work. Thank you both though for your help
mr_evans2u
03-26-2003, 11:26 PM
I got it. Thanks guys
submit(-name=>"Save_Send",
-value=>"Submit Filter Modifications",
-onClick=>"onbeforeunload = null;return checkAllItems(this.form)",
cheesebagpipe
03-27-2003, 12:17 AM
Don't think that gets it...what happens if the validator returns false? No more prompt. Try this:
<script type="text/javascript">
function killprompt() {
onbeforeunload = null;
return true;
}
</script>
</head>
<body>
<FORM>
<input type="submit" value="Submit Filter Modifications" onclick="return(checkAllItems(this.form)&&killprompt())" />
The delete operator doesn't 'clear' variables - it destroys them. It cannot be used to destroy predefined properties (like HTML event handlers); Roy Sinclair's approach was correct.
liorean
03-27-2003, 12:23 AM
Well, it works on window.oncontextmenu, window.onkeypress, window.onmousedown etc. to remove no-right-click scripts.
cheesebagpipe
03-27-2003, 12:40 AM
<html>
<head>
<script type="text/javascript">
onbeforeunload = function() {}
tryit = function() {delete onbeforeunload;}
</script>
</head>
<body>
<a href="javascript:void tryit()">try it</a>
</body>
</html>
liorean
03-27-2003, 12:54 AM
Ok - see what I get for using moz all the time? In moz you can use delete on event handlers, in ie you can't. I guess onbeforeunload=null is best after all ;-)
Roy Sinclair
03-27-2003, 02:39 PM
The key thing to remember about an event handler is that it's a object reference to a function. Unfortunately nobody writes their code this way but it's easy for a function that wants to hook an event to save any existing object reference, replace it with their own and as a last check before ending their processing call the saved function and merge it's return value with your own return value (for objects that allow cancelling). All these scripts which have conflicting onload handlers could easily be updated to never conflict and always work automatically.
This isn't a new capability either, it works in IE 4 and Netscape 4.
Originally posted by Roy Sinclair
The key thing to remember about an event handler is that it's a object reference to a function. Unfortunately nobody writes their code this way but it's easy for a function that wants to hook an event to save any existing object reference, replace it with their own and as a last check before ending their processing call the saved function and merge it's return value with your own return value (for objects that allow cancelling). All these scripts which have conflicting onload handlers could easily be updated to never conflict and always work automatically.
This isn't a new capability either, it works in IE 4 and Netscape 4.
Or one can just use addEventListener to resolve this entire problem.
liorean
03-27-2003, 06:24 PM
Well, not in a real-world situation. In a real-world situation attachEvent gets larger user base coverage.
You could do something like what Chris Nott of Dithered (http://ditgered.com/) does in his latest entry (http://dithered.com/archives/200303.html#14_2).
Roy Sinclair
03-27-2003, 06:32 PM
Originally posted by jkd
Or one can just use addEventListener to resolve this entire problem.
Yes but that IS a new capability and won't be found on older browsers that you may have to support. However, if your audience is using current browsers then that is definitely the way to go.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.