Is there any idea how to prevent the next event to happen?

S

sowencheung

I attach an Event in document.onkeyup and another event in
document.onbeforeunload

in onkeyup event, if the condition is satisfied, i will activate sth,
then i don't want the onbeforeunload event to happen.

is it possible to accomplish this?

thanks!
 
E

Evertjan.

wrote on 07 apr 2006 in comp.lang.javascript:
I attach an Event in document.onkeyup and another event in
document.onbeforeunload

in onkeyup event, if the condition is satisfied, i will activate sth,
then i don't want the onbeforeunload event to happen.

is it possible to accomplish this?

In general the "event" in the sense of a key or mouse action can only be
prevented by forcefully restraining the user. ;-)

You can however acccomplsh your aim by changing the execution to a non
execution. [Not to a non-event]

var preventive = false;

function oneventOnkeyup (){
preventive = true
// do what you want with this event;
}

function oneventOnbeforeunload(){
if (preventive) return;
// do what you want with this event;
}
 
T

Thomas 'PointedEars' Lahn

I attach an Event in document.onkeyup and another event in
document.onbeforeunload

in onkeyup event, if the condition is satisfied, i will activate sth,
then i don't want the onbeforeunload event to happen.

is it possible to accomplish this?

That would depend on what you call "attach an Event". It is possible to
prevent the _event listener_ from being called (that is not preventing the
event from happening), by assigning `null' to the event handler property
if the event listener was assigned with `targetObject.onbeforeunload =
functionObjRef;':

targetObject.onbeforeunload = null;

Or IE-only, if the event listener was assigned to the target object for the
event with targetObject.attachEvent("onbeforeunload", functionObjRef):

targetObject.detachEvent("onbeforeunload", functionObjRef);

(There is no mention of a standards compliant `beforeunload' event yet,
so the methods of W3C DOM Level 2 Event should not apply, and it is
error-prone to use them here.)

Another approach is to set a flag to prevent pieces of code in the event
listener from being executed, but the event listener will be called anyway.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top