I have some jQuery code to check all links on the site and return a disclaimer if it is an external link. Works fine. I do NOT want the disclaimer to appear for any embedded youtube videos on the site. I want to alter the code so this happens. So, if link does NOT contain youtube.com, then execute.
This is the original working code:
$(document).ready(function(){
$('a').filter(function() {
return this.hostname && this.hostname !== location.hostname;
})
.click(function () {
var x=window.confirm("You are now leaving our website. Click OK to proceed or cancel to stay on this page.");
var val = false;
if (x)
val = true;
else
val = false;
return val;
});
});
I'd like to insert the code below code that defines if link is NOT youtube, but I don't know where to put it in the original code. I am new to jQuery. Thank you for any help.
if $('a').not('[href^="//www.youtube"], [href^="https://www.youtube"]');
Just chain the .not()
after your .filter()
. Also you've got a lot of redundant code in the click confirmation, so I cleaned that up too:
$(document).ready(function(){
$('a').filter(function() {
return this.hostname && this.hostname !== location.hostname;
}).not('[href^="//www.youtube"], [href^="https://www.youtube"]').click(function () {
return confirm("You are now leaving our website. Click OK to proceed or cancel to stay on this page.");
});
});