Skip to content

Instantly share code, notes, and snippets.

@hsquareweb
Forked from rjv/external-new-window
Created January 9, 2014 17:04
Show Gist options
  • Save hsquareweb/8337813 to your computer and use it in GitHub Desktop.
Save hsquareweb/8337813 to your computer and use it in GitHub Desktop.
// 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