Capture user response when using [removed]

前端 未结 4 1798
遇见更好的自我
遇见更好的自我 2020-12-10 11:59

Is there a way to capture whether the user clicked OK or CANCEL?

I need to do something ONLY if the user is leaving the page....

相关标签:
4条回答
  • 2020-12-10 12:12

    No, browsers allow you to only specify the text in the alert box, but no way to catch the result.

    0 讨论(0)
  • 2020-12-10 12:12

    This is a programming trick or rather a workaround which you can try. I did not find any "javascript provided" default ways to do it

    I have a webpage (it has a form in it) sitting on a local system which needs to be protected from unauthorized users from entering any inputs to it.

    I have created a JavaScript function which loads along with loading the main page.

    like below

    <script src="./resources/brazil-uam-all-javascript.js"></script>
      </head>
    <body id="uam_gui" onload="authenticate2();">
    <div id="UAM_tool">
    

    NOW THE JAVASCRIPT FUNCTION

    The user is supposed to enter a value when prompted with a dialogue box. If the user does not enter any value and simply tries to bypass it by clicking on OK or CANCEL or uses the ESC key or the "Close" button to avoid authentication, then page will bounce back with the same prompt. Here I am indirectly capturing the user's response. Hope it help's your requirement.

    function authenticate2() {
    var prompt_for_the_magic_number = prompt("Enter Your                         M  A  G  I  C    N  U  M  B  E  R     :::   ", "");
      if (prompt_for_the_magic_number === true)
      {
          if (prompt_for_the_magic_number.length === 0)
          {
              alert("You are supposed to ENTER A VALUE");
              location.reload();
          }
          else
          {
              if (prompt_for_the_magic_number.length > 4)
              {
                  alert("You are supposed to ENTER ONLY 4 DIGITS");
                  location.reload();
              }
              esle
              {
                  //some code to allow access based on a logic
              }
          }
      }
      else
      {
          alert("You are supposed to ENTER 4 DIGITS");
          location.reload();
      }
    }
    
    0 讨论(0)
  • 2020-12-10 12:30

    You can use one setTimeout to verify.

    window.onbeforeunload = function() {
        if (needToConfirm) {
            setTimeout(function() {
                alert('User still is on the page');
            }, 0);
            return 'Are you sure you want to leave?';
        }
    };
    

    Basically, the onbeforeunload function blocks the execution and the timeout will only be executed if the user still on the page. The 0 milliseconds is only to put it to the bottom of the execution queue.

    0 讨论(0)
  • 2020-12-10 12:31

    Here is the solution I am using in a situation where I need to do something (such as clear the session) ONLY when the user navigates from the page.

    I have 2 global vars

    var clearSession = true;
    var confirmExit = true;
    
    
        window.onbeforeunload = function() { return confirmExit(); }
        window.onunload = function() { return clearSession(); }
    
    function confirmExit() {
        if (needToConfirm == true) {       
    
            return "exit page?";
        }
    }
    
    
    function clearSession() {
    
         if (clearSession == true) {
            alert("Killing the session on the server!!!");
            PageMethods.ClearSession();
        }
    }
    

    Then of course, on every page submit/button/drop down list etc...you need to make sure that the above global variables are set to false.

    Hope this helps someone.

    0 讨论(0)
提交回复
热议问题