SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Confirm help...

    I'm working on a "Document Depo" for pdf storage (using Coldfusion CFFILE tags to upload/move/save ...blah. blah...)

    If a client tries to load a document into the system that has the same filename of an existing file, I wish to give them the option to overwrite or cancel out.

    Coldfusion checks for the existing file, and if it's there, I do this:

    <script type="text/javascript">
    var OverWrite=confirm("Another File with This Name Already Exists. Overwrite It?")
    if (!OverWrite)
    history.go(-1);
    </script>

    The problem is - hitting cancel works - it takes them back to the form and they can chose what to do from there.
    Hitting "OK" ... I need the page to continue to process ... but it's not. It just stops there and doesn't do anything else.

    What am I missing from my javascript above?

    Thanks ...

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,811
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You are using a debugging command to try to achieve something - you should use a form in the web page itself to make the selection instead.

    The confirm debugging call offers you three options not just two with the third (depending on which browser you are using) either disabling all dialogs for your web page or turning off JavaScript for your page - either of which ensure that no further confirms will appear.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    In addition to what Stephen said, I just wanted to add that I find the behaviour you describe strange.

    When a confirm box pops up, as you know, the user will have to click either "OK" or "Cancel" to proceed.
    If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.

    In the context of processing a form, if handled correctly, a return value of false should return you to the form, whereas a return value of true should not interfere with the flow of execution.

    Here's a very simple example to demonstrate what I mean:

    HTML Code:
    <!DOCTYPE HTML>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Confirm example</title>
      </head>
    
      <body>
        <form id="myForm">
          <input type="submit">
        </form>
        
        <script>
          function logNumbersTo(num){
            var conf=confirm("Are you sure about  this?")
            if (conf){
              // Continue processing
              for(var i=0; i<num; i++){
                console.log(i);
              }
            }
          }
          
          f = document.getElementById("myForm");
          f.onsubmit = function(){
            logNumbersTo(10);
            return false;
          }
        </script>
      </body>
    </html>
    Perhaps I'm missing something.
    Could you provide some code or a link to the page in question?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •