admin管理员组文章数量:1394578
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.
本文标签:
版权声明:本文标题:javascript - How can I change this code so that when the user clicks cancel on the pop up the link does not open? - Stack Overfl 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744628876a2616419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论