onbeforeunload issue, Mozilla Firefox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alistair Potts

    #1

    onbeforeunload issue, Mozilla Firefox

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

    window.onbefore unload=checkFor m;
    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
  • Martin Honnen

    #2
    Re: onbeforeunload issue, Mozilla Firefox



    Alistair Potts wrote:
    [color=blue]
    > I'm assigning the onbeforeunload event a function thus (pseudo code):
    >
    > window.onbefore unload=checkFor m;
    > 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.[/color]

    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.opti ons.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?


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Alistair Potts

      #3
      Re: onbeforeunload issue, Mozilla Firefox

      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



      Martin Honnen wrote:[color=blue]
      >
      >
      > Alistair Potts wrote:
      >[color=green]
      >> I'm assigning the onbeforeunload event a function thus (pseudo code):
      >>
      >> window.onbefore unload=checkFor m;
      >> 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.[/color]
      >
      >
      > 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.opti ons.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?
      >
      >[/color]

      Comment

      • Martin Honnen

        #4
        Re: onbeforeunload issue, Mozilla Firefox



        Alistair Potts wrote:
        [color=blue]
        > 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!).[/color]

        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.

        --

        Martin Honnen
        http://JavaScript.FAQTs.com/

        Comment

        • Alistair Potts

          #5
          Re: onbeforeunload issue, Mozilla Firefox

          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



          Martin Honnen wrote:[color=blue]
          >
          >
          > Alistair Potts wrote:
          >[color=green]
          >> 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!).[/color]
          >
          >
          > 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.
          >[/color]

          Comment

          • Martin Honnen

            #6
            Re: onbeforeunload issue, Mozilla Firefox



            Alistair Potts wrote:
            [color=blue]
            > 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.[/color]

            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.

            --

            Martin Honnen
            http://JavaScript.FAQTs.com/

            Comment

            Working...