Reputation: 1955
I have an application where the user enters a good deal of information. Accordingly, it is important to use the beforeunload
event so that the user doesn't accidentally leave the page and lose their progress. However, the app also requires download links and the problem is that clicking those download links triggers the beforeunload event even though the download does not cause the user to leave the page.
How can I set this up so that the the user has to confirm before leaving the page but that this confirmation is not triggered by clicking a download link?
You can see the whole thing in a reproducible example here:
Live page: https://wqy9kn5qv7.codesandbox.io/
Source code: https://codesandbox.io/s/wqy9kn5qv7
Upvotes: 3
Views: 1047
Reputation: 6745
This is kind of a hack, but I think it will prevent the beforeunload
event from firing on your download links:
<a href="somefile.txt" download target="_blank">Download</a>
Basically, add the download
attribute to the element and a target
attribute.
Upvotes: 5
Reputation: 1
You can use required
attribute at <input>
and/or <textarea>
elements to prevent <form>
submission until the required
fields have been completed.
See
also
Upvotes: -2