Created
February 25, 2020 08:30
-
-
Save liuxd/9b2577e9c1ea678b6cf3718094cb290a to your computer and use it in GitHub Desktop.
[Bind all <a> to ask whether to go or not with a confirm popup] #JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<a href="https://www.google.com/">Google</a> | |
<a href="https://www.youtube.com/">Youtube</a> | |
<script> | |
function clickRedirectHandler(e) { | |
e.preventDefault(); | |
let url = e.target.href; | |
let ask = confirm('Do you want to go to ' + url); | |
if (ask) { | |
location.href = url; | |
} | |
} | |
function setRedirectConfirmationDialogs() { | |
document.querySelectorAll('a').forEach(e => e.addEventListener('click', clickRedirectHandler)) | |
} | |
setRedirectConfirmationDialogs() | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment