Code snippets to force external links to open in a new tab/window ***************************************************************** $('a').filter(function() { return this.hostname && this.hostname !== location.hostname; }).addClass("external"); *************************************************************************** $('a').each(function() { var a = new RegExp('/' + window.location.host + '/'); if (!a.test(this.href)) { // This is an external link } }); ************************************************************ $('a').each(function() { var a = new RegExp('/' + window.location.host + '/'); if(!a.test(this.href)) { $(this).click(function(event) { event.preventDefault(); event.stopPropagation(); window.open(this.href, '_blank'); }); } }); ************************************************************** /** * Open all external links in a new window */ jQuery(document).ready(function($) { $('a') .filter('[href^="http"], [href^="//"]') .not('[href*="' + window.location.host + '"]') .attr('rel', 'noopener noreferrer') .attr('target', '_blank'); }); ************************************************************************ // vanilla JavaScript var links = document.links; for (var i = 0, linksLength = links.length; i < linksLength; i++) { if (links[i].hostname != window.location.hostname) { links[i].target = '_blank'; } } *************************************************************************** // or in jQuery $(document.links).filter(function() { return this.hostname != window.location.hostname; }).attr('target', '_blank'); *************************