Skip to content

Instantly share code, notes, and snippets.

@azone
Created January 13, 2013 11:33
Show Gist options
  • Save azone/4523641 to your computer and use it in GitHub Desktop.
Save azone/4523641 to your computer and use it in GitHub Desktop.
Open Octopress external link in new tab
function addBlankTargetForLinks () {
$('a[href^="http"]').each(function(){
$(this).attr('target', '_blank');
});
}
$(document).bind('DOMNodeInserted', function(event) {
addBlankTargetForLinks();
});
@androidyue
Copy link

I did a little change. Only add _blank to those links with no target attribute.
What's more, you should note that jquery is required to make the code running.

$(document).bind('DOMNodeInserted', function(event) {
        $('a[href^="http"]').each(
            function(){
                if (!$(this).attr('target')) {
                    $(this).attr('target', '_blank')
                }

            }
        );
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment