How can I force links to automatically open in a new window?

Normally, if I want to force a link to open in a new tab (or window) when posting to my blog, I have to either use the link GUI and select “open in new window”, or, since I use the HTML view by default, after inserting a link, manually add the “target=” portion of the tag:

<a href="http://link.to/something.great" target="_blank">link text</a>

Is there a plugin or hook location I can use to automatically insert the target attribute, or am I stuck doing it manually?

Read More

EDIT: I am looking specifically for a way to modify the link while I am creating it in the editor. If that isn’t possible, then maybe a hack on the save process. But I don’t want a “run-time” front-end hack, which isn’t necessarily permanent.

Related posts

Leave a Reply

4 comments

  1. If you use jQuery 1.3+ you can easily do this with the following line of JavaScript:

    $("a:not([href^='http://your.website-url.here']").attr('target', '_blank');
    

    Just add this to the load() event of jQuery.

  2. You could do it using javascript fairly easily. Are you wanting to set target on all external links? Or just the ones within the post body?

    Either way, here’s the jQuery code to do it:

    $(document).ready(function(){
        $("#postBody a").attr('target','_blank');
    });
    

    Assuming your post body is inside a div with the ID “postBody”.