Skip to content

Instantly share code, notes, and snippets.

@azone
Created January 13, 2013 11:33
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • 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();
});
@lishunli
Copy link

tks share

@tobiashochguertel
Copy link

Thanks.
A little improvement for my requirements, I want to be able to control which hyperlink get an target attribute.

Markdown document example snippet:

[read this resource.][topic-name-resources-id1]

[topic-name-resources-id1]: https://google.de "#ext: read this resource."

open-in-blank.js:

function addBlankTargetForLinks () {
  $('a[title^="#ext"]').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