I am extremely new to JavaScript but I am tackling a project to make a google chrome extension that detects when a user clicks a link and asks them whether or not they want to continue. The issue is that currently when the pop-up appears, the link will open regardless of which option the user picks. This is the code I am currently working with.
// detects when the user clicks.
document.addEventListener("click", function(event) {
//checks if a user clicks on a hyperlink specifically.
const target = event.target.closest("a")
//This section makes it so the link is not immediately opened.
if (target) {event.preventDefault();
// Makes the pop up and shows the full link to the user, they decide if they want to proceed.
if (confirm("You clicked on a link!"+"\nAre you confident you want to proceed?\n\n\n" + target.href)) {
window.location.href = target.href;
}
}
}, true);
I've looked through a few different forums and FAQs for a good answer but I haven't seen anyone with the same problem. I have messed with the
window.location.href = target.href;}}}, true);
phrase a bit but I cant seem to figure out how to fix this in order to make it actually cancel the click on the link when I hit cancel.