Skip to content

Instantly share code, notes, and snippets.

@liuxd
Created February 25, 2020 08:30
Show Gist options
  • Save liuxd/9b2577e9c1ea678b6cf3718094cb290a to your computer and use it in GitHub Desktop.
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
<!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