Created
May 2, 2012 12:53
-
-
Save markwearing/2576339 to your computer and use it in GitHub Desktop.
Open all external links in a new tab/window
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
| // Wait until the DOM is fully loaded before executing the jQuery | |
| $(document).ready(function($) { | |
| // Lets open all external links in a new tab | |
| $.extend ($.expr[':'], { | |
| internal: function (e) { | |
| if (e.nodeName.toLowerCase() != 'a') { | |
| return false; | |
| } | |
| var href = $(e).attr("href"); | |
| if (href.match(/^http:\/\//)) { | |
| var parts = href.split('/'); | |
| if (parts[2] !== location.host) { | |
| return false; | |
| } | |
| } | |
| if (href.match(/^mailto:/)) { | |
| return false; | |
| } | |
| if (href.toLowerCase().match(/.(pdf|doc|xls|docx|xlsx)/)) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| }); | |
| $("a:not(:internal)").click (function (e) { | |
| window.open ($(this).attr("href")); | |
| e.preventDefault (); | |
| return false; | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment