Created
June 30, 2010 13:29
-
-
Save abhiomkar/458640 to your computer and use it in GitHub Desktop.
Make external links to load in the new window (or tab). Doesn't work if the href is the javascript function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name top2blank | |
| // @description Set External links' target value to _blank | |
| // @namespace http://abhiomkar.in | |
| // @include http://* | |
| // @include https://* | |
| // ==/UserScript== | |
| aTags = document.getElementsByTagName("a"); | |
| for(i=0; i< aTags.length; i++) { | |
| protocol = window.location.protocol+"//"; | |
| host = window.location.hostname; | |
| url = aTags[i].href; | |
| if(url.indexOf("http://") == 0 || url.indexOf("https://") == 0){ | |
| // This may be internal link or external link | |
| if (url.indexOf("http://") == 0){ | |
| url = url.replace("http://",""); | |
| } | |
| else if(url.indexOf("https://") == 0){ | |
| url = url.replace("https://",""); | |
| } | |
| if( url.indexOf("/") >= 0 ){ | |
| // Example: http://google.com/images | |
| url = url.substring(0, url.indexOf("/")); | |
| if( url == host ){ | |
| // Internal link | |
| // Do Nothing | |
| } | |
| else{ | |
| // Yah!! External link | |
| // change the target attribute to _blank | |
| // console.log("Changing " + aTags[i].href + " to _blank"); | |
| aTags[i].target = "_blank"; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment