I have the following (partial) code:
<input type="checkbox" id="myCheck">
span class="checkmarkz"></span>
<a href="test.zip" class="download" onclick="return ifchecked()">One-time download!</a>
<script>
function ifchecked() {
if (document.getElementById('myCheck').checked) {
return true;
} else {
let warning = document.getElementById('warning');
warning.style.display = "block";
return false;
}
}
</script>
I would need that after the download, the user is redirect to success.php.
NOTE: I know there are other questions were users ask for redirect after download link, but in this case I have already onclick="return ifchecked()", then I don't know how to or where I could add a redirect since I need to check the checkbox first. Please consider this before giving me -1 or -2 or -3. Please be kind. Thank you.
Don't use inline HTML event attributes in the first place as this is a 25+ year old event wiring technique that just will not die because people just keep copying other people's code.
Instead, use the modern approach, which is
.addEventListener(). With this method, you can register multiple event listeners to the same event and the listeners will fire in the order that you've registered them.Here's an example:
But, in your use case, you don't seem to need multiple callbacks. You'd do something like this:
Keep in mind that this code won't actually prevent someone from downloading the file more than once if they really want to. All they'd have to do is look at the source code of the page and see the path and file name of the download file and then manually navigate to that location. If you really want to secure the file, you'd need some kind of server-side validation.