On leaving page

Discussion in 'Javascript' started by UKuser, Sep 7, 2007.

  1. UKuser

    UKuser Guest

    Hi folks,

    Can anyone tell me why this is not working, where the script is placed
    in the header section and document.pressed=this.value; is in the
    onClick statement of a form submit button.

    window.onbeforeunload =
    function OnSubmitForm(){
    if (document.pressed='Save1'){
    return true;
    } else {
    return("You will lose any unsaved info");
    }
    }

    I'm trying to get it so that you only get the error when you make a
    move other than clicking the Continue button.

    Any help would be great, as I'm not a javascript guru.

    Thanks
    UKuser, Sep 7, 2007
    #1
    1. Advertising

  2. UKuser schreef:
    > window.onbeforeunload =
    > function OnSubmitForm(){
    > if (document.pressed='Save1'){
    > return true;
    > } else {
    > return("You will lose any unsaved info");
    > }
    > }
    >


    I don't know the document.pressed property (custom one?), but if you do
    it as follows, it might work:

    function OnSubmitForm() {
    if (document.pressed == 'Save1'){ // '=' is an assignment
    return true;
    } else {
    return("You will lose any unsaved info");
    }
    }

    window.onbeforeunload = OnSubmitForm;


    JW
    Janwillem Borleffs, Sep 7, 2007
    #2
    1. Advertising

  3. UKuser

    Bill H Guest

    On Sep 7, 6:50 am, UKuser <> wrote:
    > Hi folks,
    >
    > Can anyone tell me why this is not working, where the script is placed
    > in the header section and document.pressed=this.value; is in the
    > onClick statement of a form submit button.
    >
    > window.onbeforeunload =
    > function OnSubmitForm(){
    > if (document.pressed='Save1'){
    > return true;
    > } else {
    > return("You will lose any unsaved info");
    > }
    > }
    >
    > I'm trying to get it so that you only get the error when you make a
    > move other than clicking the Continue button.
    >
    > Any help would be great, as I'm not a javascript guru.
    >
    > Thanks


    I think you want to return false, not an error message. I could be
    wrong, but doesnt the submit handler just look for a value of true or
    false and default to true if false is not false?

    Bill H
    Bill H, Sep 7, 2007
    #3
  4. Janwillem Borleffs schreef:
    > function OnSubmitForm() {
    > if (document.pressed == 'Save1'){ // '=' is an assignment
    > return true;
    > } else {
    > return("You will lose any unsaved info");
    > }
    > }
    >
    > window.onbeforeunload = OnSubmitForm;
    >


    Bill H is right about his comment, so the OnSubmitForm function can be
    reduced into the following:

    function OnSubmitForm() {
    return document.pressed == 'Save1';
    }


    JW
    Janwillem Borleffs, Sep 7, 2007
    #4
  5. Bill H wrote:
    > On Sep 7, 6:50 am, UKuser <> wrote:
    >> Can anyone tell me why this is not working, where the script is placed
    >> in the header section and document.pressed=this.value; is in the
    >> onClick statement of a form submit button.
    >>
    >> window.onbeforeunload =
    >> function OnSubmitForm(){
    >> if (document.pressed='Save1'){
    >> return true;
    >> } else {
    >> return("You will lose any unsaved info");
    >> }
    >> }
    >>
    >> I'm trying to get it so that you only get the error when you make a
    >> move other than clicking the Continue button.
    >>
    >> Any help would be great, as I'm not a javascript guru.

    >
    > I think you want to return false, not an error message.


    No, that approach is correct (to a certain extent). A peculiarity of the
    proprietary `onbeforeunload' event handler is that if the return value is
    a string value, it is displayed in a message box asking whether the user
    actually wants to navigate away. As a precaution against scriptkiddies,
    that event cannot be canceled anymore (IIRC that change was made with
    the Firefox 2.0 release). Returning `true' or another non-string value
    displays the same as returning the empty string (at least in my Firefox
    2.0.0.6 on Windows XP SP2).

    > I could be wrong, but doesnt the submit handler just look for
    > a value of true or false and default to true if false is not false?


    There is no event handler for the `submit' event here.

    Different event handlers require different return values. However, either
    `true' or `false' are usually the values required to return in order to
    prevent the default action and further bubbling (to cancel the event) this
    way, depending on the event handler. For the `submit' event, it may be
    possible only to return `false' to cancel the event and return another
    true-value otherwise; however, I would always make that little effort and
    return `true' in the latter case instead just to be on the safe side.


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
    Thomas 'PointedEars' Lahn, Sep 7, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rich
    Replies:
    1
    Views:
    393
    bruce barker
    Feb 20, 2004
  2. brian

    Leaving a Page

    brian, Jun 11, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    3,546
    Justin Beckwith
    Jun 12, 2004
  3. UJ
    Replies:
    4
    Views:
    2,774
    Curt_C [MVP]
    Aug 10, 2005
  4. Replies:
    7
    Views:
    25,908
    rohit.net100
    Feb 28, 2011
  5. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    Capturing the leaving of a page/http_request

    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Nov 17, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    472
    Karl Seguin
    Nov 17, 2005
Loading...

Share This Page