diff --git a/tildes/static/js/behaviors/external-links-new-tabs.js b/tildes/static/js/behaviors/external-links-new-tabs.js index 00ac324..d89180f 100644 --- a/tildes/static/js/behaviors/external-links-new-tabs.js +++ b/tildes/static/js/behaviors/external-links-new-tabs.js @@ -1,7 +1,17 @@ $.onmount('[data-js-external-links-new-tabs]', function() { - // Make links in the text open in a new tab - $(this).find( - 'a[href^="http"]' + // Select external links - ':not([href*="tildes.net"])' // But not links to tildes - ).attr('target', '_blank'); + /* Open external links in topic, comment, and message text in + * new tabs. + * + * The regex matches links starting with "/" or "tildes.net", which are + * "internal" links to Tildes pages (posts, topics, users, etc). All + * other links are considered "external" and open in a new tab. + */ + let re = /^((?!\/).)$|^https?:\/\/((?!tildes\.net).).*$/gm; + // Loop through links + $(this).find('a').each(function() { + // Check if href is external + if (re.exec(this.href) !== null) { + $(this).attr('target', '_blank'); + } + }); });