onbeforeunload issue, Mozilla Firefox

A

Alistair Potts

I'm assigning the onbeforeunload event a function thus (pseudo code):

window.onbeforeunload=checkForm;
function checkForm() {
if(formChanged){ return 'You changed the form, are you sure?' }
}

As a conditional unload it works fine - the prompt only appears if you
have changed form values. The issue is that Firefox/Mozilla comes up
with a warning - "function checkForm does not always return a value".

This is really annoying me - I wondered if anyone had a workaround. If
you return anything else (null, false etc.) you get the prompt.

AJP
 
M

Martin Honnen

Alistair said:
I'm assigning the onbeforeunload event a function thus (pseudo code):

window.onbeforeunload=checkForm;
function checkForm() {
if(formChanged){ return 'You changed the form, are you sure?' }
}

The issue is that Firefox/Mozilla comes up
with a warning - "function checkForm does not always return a value".

This is really annoying me - I wondered if anyone had a workaround. If
you return anything else (null, false etc.) you get the prompt.

The warning shouldn't annoy you, it is just an attempt to do some more
compiler like checking in a scripting language.
It should also only appear with the preference javascript.options.strict
set to true.

Even with that set to true I cannot reproduce the problem with Firefox
1.0. Which version of FF are you trying with?
 
A

Alistair Potts

v1.0.3; you'll only see the problem with strict mode on, through a XHTML
doctype.

I appreciate it's a 'warning', but I've been careful to code without any
warnings (I get a smiley face with the web developer toolbar!).

I appreciate your feedback. Alistair
 
M

Martin Honnen

Alistair said:
v1.0.3; you'll only see the problem with strict mode on, through a XHTML
doctype.

I appreciate it's a 'warning', but I've been careful to code without any
warnings (I get a smiley face with the web developer toolbar!).

If you want to avoid the warning I guess you can do
if (condition) {
return 'warning message';
}
else {
return (void 0);
}
Let us know whether that avoids the warning.
 
A

Alistair Potts

That's fantastic!

For those who don't know: "The particularity of void is, that it
evaluates the given expression, but does not return a value as a
result." So it's perfect for this situation.

AJP
 
M

Martin Honnen

Alistair said:
For those who don't know: "The particularity of void is, that it
evaluates the given expression, but does not return a value as a
result." So it's perfect for this situation.

void 0 gives you the value undefined so semantically
return (void 0);
is the same as
return undefined;
which is the same as
return;
but obviously it goes unnoticed by Spidermonkey's warning generator.
 

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
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top