You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.1 KiB

  1. // Works like jQuery's $(document).ready.
  2. // Supports IE8+. Courtesy of http://youmightnotneedjquery.com/
  3. function 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. ready(function() {
  16. var website = window.location.hostname;
  17. var internalLinkRegex = new RegExp('^((((http:\\/\\/|https:\\/\\/)(www\\.)?)?'
  18. + website
  19. + ')|(localhost:\\d{4})|(\\/.*))(\\/.*)?$', '');
  20. var anchorEls = document.querySelectorAll('a');
  21. var anchorElsLength = anchorEls.length;
  22. for (var i = 0; i < anchorElsLength; i++) {
  23. var anchorEl = anchorEls[i];
  24. var href = anchorEl.getAttribute('href');
  25. if (href[0]=="#") {
  26. // if its an internal link, do not add the 'target=_blank'
  27. continue;
  28. }
  29. if (!internalLinkRegex.test(href)) {
  30. anchorEl.setAttribute('target', '_blank');
  31. }
  32. }
  33. });