Browse Source
Use link host to find external links
merge-requests/14/head
Ivan Fonseca
7 years ago
No known key found for this signature in database
GPG Key ID: 95A9C856EC788689
1 changed files with
3 additions and
12 deletions
-
tildes/static/js/behaviors/external-links-new-tabs.js
|
|
|
@ -1,17 +1,8 @@ |
|
|
|
$.onmount('[data-js-external-links-new-tabs]', function() { |
|
|
|
/* 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
|
|
|
|
// Open external links in topic, comment, and message text in new tabs
|
|
|
|
$(this).find('a').each(function() { |
|
|
|
// Check if href is external
|
|
|
|
if (re.exec(this.href) !== null) { |
|
|
|
$(this).attr('target', '_blank'); |
|
|
|
if (this.host !== window.location.host) { |
|
|
|
$(this).attr('target', '_blank'); |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |