Open External Links in New Tab

Opens all external links in a new tab

  1. // ==UserScript==
  2. // @name Open External Links in New Tab
  3. // @version 1.0
  4. // @description Opens all external links in a new tab
  5. // @match *://*/*
  6. // @grant none
  7. // @license MIT
  8. // @author Howard D. Lince III
  9. // @supportURL https://twitter.com/HowardL3
  10. // @namespace https://github.com/howard3
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Get all links in the document
  17. var links = document.getElementsByTagName('a');
  18.  
  19. // Loop through each link
  20. for (var i = 0; i < links.length; i++) {
  21. // Check if the link's hostname is different from the current page's hostname
  22. if (links[i].hostname !== window.location.hostname) {
  23. // Change the target attribute to _blank
  24. links[i].target = '_blank';
  25. }
  26. }
  27. })();