How to close a window after form is submitted?

Discussion in 'Javascript' started by Seth E Seligman, Jun 29, 2003.

  1. I'm working on a web based file manager. The rename file function creates a new window with a form allowing the user to enter a new file name. Before the form gets submitted, I want to make sure that a valid file name (no "/" or "\" characters) was entered. If the validation functions returns true, the form should be submitted and the window closed. If the function returns false the form window should stay open so the use can enter a new file name. What's the correct way to do this? When I try to add a win
    dow.close function I either close the window without submitting the form, or submit the form without closing the window.
    Seth E Seligman, Jun 29, 2003
    #1
    1. Advertising

  2. (Seth E Seligman) writes:

    > I'm working on a web based file manager. The rename file function
    > creates a new window with a form allowing the user to enter a new
    > file name. Before the form gets submitted, I want to make sure that
    > a valid file name (no "/" or "\" characters) was entered. If the
    > validation functions returns true, the form should be submitted and
    > the window closed. If the function returns false the form window
    > should stay open so the use can enter a new file name. What's the
    > correct way to do this?


    > When I try to add a win dow.close function I either close the window
    > without submitting the form, or submit the form without closing the
    > window.


    To validate a form field, create a function that returns true it
    the field contains a legal value and false if it doesn't. E.g.

    <script type="text/javascript">
    function validateField(form) {
    var result = true;
    var response = "";

    var fieldValue1 = form.elements['fieldName'].value;
    if (fieldValue1.match(/[\\\/]/)) { // contains \ or /
    result = false;
    response += "Filename may not containt \"\\\" or \"/\".\n";
    }

    if (!result) {
    alert(response);
    }
    return result;
    }
    </script>

    Then call the function from the form's onsubmit handler, and return
    its result:

    <form ... onsubmit="return valudateField(this)">


    To close the window after the form has been sumitted, you should wait
    until you are certain that the form is successfully submitted. That
    means that you should close the window in the page that is returned by
    the form submit, not in the one containing the form. (You should
    ofcourse have a cancel button too.

    If the popup window isnt doing anything except querying for a filename,
    you could look into the "prompt" function.

    /L
    --
    Lasse Reichstein Nielsen -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jun 29, 2003
    #2
    1. Advertising

  3. Seth E Seligman

    John Guest

    (Seth E Seligman) wrote in message news:<3efe5987$0$3949$>...
    > I'm working on a web based file manager. The rename file function

    creates a new window with a form allowing the user to enter a new file
    name. Before the form gets submitted, I want to make sure that a valid
    file name (no "/" or "\" characters) was entered. If the validation
    functions returns true, the form should be submitted and the window
    closed. If the function returns false the form window should stay open
    so the use can enter a new file name. What's the correct way to do
    this? When I try to add a win
    > dow.close function I either close the window without submitting the form, or submit the form without closing the window.


    At least in internet-divergent internet explorer, the close() method
    preempts any form submission as if the user closed the window. It is
    not programmatically dependent on the return of any previous
    instructions. So, window.close() closes the window, suddenly, and
    halting whatever may have been processing, which can be useful. The
    onSubmit seems to return status immediately, allowing the next
    instruction.

    Consider having the form handler do the window closing. It would know
    when the posting process is complete.
    John, Jun 29, 2003
    #3
    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. Iñaki Baz Castillo
    Replies:
    7
    Views:
    824
    Iñaki Baz Castillo
    Jan 12, 2010
  2. Kwok Ng
    Replies:
    1
    Views:
    115
  3. Rasmus
    Replies:
    2
    Views:
    86
    kaeli
    Dec 2, 2003
  4. Shang Wenbin

    close current window using window.close()

    Shang Wenbin, Sep 1, 2005, in forum: Javascript
    Replies:
    10
    Views:
    357
    Gérard Talbot
    Sep 2, 2005
  5. hon123456
    Replies:
    3
    Views:
    290
    Evertjan.
    Jan 9, 2007
Loading...

Share This Page