Advertisement
Guest User

Link To New Tab

a guest
Jan 5th, 2022
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Link To New Tab
  3. // @namespace http://*
  4. // @description Opens links external to the current site in a new tab
  5. // @include http*
  6. // ==/UserScript==
  7.  
  8. // Ignore links with relative URLs
  9. // and links to the current site
  10.  
  11. function linkToNewTab() {
  12. var anchors = document.getElementsByTagName("a");
  13. for(var i = 0; i < anchors.length; i++) {
  14. if(anchors[i].href.indexOf("http") == 0
  15. && anchors[i].href.indexOf(window.location.host) == -1) {
  16. anchors[i].target = "_blank";
  17. }
  18. }
  19. }
  20.  
  21. window.addEventListener("load",function(){linkToNewTab()},false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement