Skip to content

Instantly share code, notes, and snippets.

@rjv
Last active January 2, 2016 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rjv/8337705 to your computer and use it in GitHub Desktop.
Save rjv/8337705 to your computer and use it in GitHub Desktop.
Open external links in a new window
// create custom selector for external links
$.expr[':'].external = function(obj){
var $this = $(obj);
return !$this.is('[href^="mailto:"]') && !$this.is('[href^="javascript:"]') && (obj.hostname != location.hostname) && ($this.data('target') != "top");
};
// DOM is ready
$(function(){
// apply target="_blank" to each external link on the page
$("a[href]:external").attr("target", "_blank");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment