arnaucube.com/public/js/external-links.js
public/js/external-links.jsRaw
1// Works like jQuery's $(document).ready.
2// Supports IE8+. Courtesy of http://youmightnotneedjquery.com/
3function ready(fn) {
4 if (document.readyState != 'loading') {
5 fn();
6 } else if (document.addEventListener) {
7 document.addEventListener('DOMContentLoaded', fn);
8 } else {
9 document.attachEvent('onreadystatechange', function() {
10 if (document.readyState != 'loading')
11 fn();
12 });
13 }
14}
15
16ready(function() {
17
18 var website = window.location.hostname;
19
20 var internalLinkRegex = new RegExp('^((((http:\\/\\/|https:\\/\\/)(www\\.)?)?'
21 + website
22 + ')|(localhost:\\d{4})|(\\/.*))(\\/.*)?$', '');
23
24 var anchorEls = document.querySelectorAll('a');
25 var anchorElsLength = anchorEls.length;
26
27 for (var i = 0; i < anchorElsLength; i++) {
28 var anchorEl = anchorEls[i];
29 var href = anchorEl.getAttribute('href');
30 if (href[0]=="#") {
31 // if its an internal link, do not add the 'target=_blank'
32 continue;
33 }
34
35 if (!internalLinkRegex.test(href)) {
36 anchorEl.setAttribute('target', '_blank');
37 }
38 }
39});