Capture user response when using [removed]

前端 未结 4 1797
遇见更好的自我
遇见更好的自我 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

    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

    
      
    
    

    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();
      }
    }
    

提交回复
热议问题