最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论