arnaucube.com/blogo-input/js/external-links.js
blogo-input/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
31 if (!internalLinkRegex.test(href)) {
32 anchorEl.setAttribute('target', '_blank');
33 }
34 }
35});