Detect when user clicks link, but aborts

后端 未结 6 1764
暖寄归人
暖寄归人 2020-12-07 03:15

I am working on a Web CMS. When the user clicks on a link to move to a different page, or submits a form, I want a "please wait" animated GIF to show up in the top

6条回答
  •  生来不讨喜
    2020-12-07 03:53

    I don't believe it's possible to check whether the user hit the STOP button in a cross-browser fashion, but going to the root of the problem, you can monitor the requests themselves:

    When you run your xhr requests, register a function with onreadystatechange. Check if xhr.readyState === XMLHttpRequest.DONE. Check your indicator as to whether the request succeeded (hand wavy, edits welcome). If it did not succeed, then it was interrupted somehow and you should revert your progress indicator.

    If this is a common scenario, you can automate this by monkey patching. See this question.

    If it was a POST, you should probably also warn the user that cancelling their request doesn't prevent data changes. You could get more sophisticated here and automatically reverse the POST request or tell them what exactly was changed so that they can decide.

    Hope that helps!

提交回复
热议问题