External Link Notification - JavaScript or JQuery

前端 未结 4 1379
隐瞒了意图╮
隐瞒了意图╮ 2021-01-26 00:31

I am looking for a way to set it up so that when an external link is clicked it will warn people that they are leaving the site. Preferably, it would darken the screen and displ

4条回答
  •  温柔的废话
    2021-01-26 01:19

    Your filter logic should be correct, Try using the confirm function, and using jQuery instead of $.

    jQuery('a').filter(function() {
        return this.hostname && this.hostname !== location.hostname;
      }).click(function(e) {
           if(!confirm("You are about to proceed to an external website."))
           {
                // if user clicks 'no' then dont proceed to link.
                e.preventDefault();
           };
      });
    

    I tried this out in dev tools on your site and it seems to work correctly if you use jQuery. I think you may have some plugin that is causing conflicts with $.

    JSFiddle Demo

提交回复
热议问题