Isaac Overacker

maker of things

Octopress Themes: Opening Links in a New Window

I just rolled out a change to the mnml theme to cause links to be opened in a new window. If you’re looking to do the same thing for your Octopress theme, it requires a simple change to the /source/javascripts/octopress.js file. Open that file and add the following to the $(document).ready() function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$(document).ready(function(){
    // You probably already have some code here.
    // Just add this to the end of the function.
    $('a').each(function() {
        var a = new RegExp('/' + window.location.host + '/');
        if(!a.test(this.href)) {
            $(this).click(function(event) {
                event.preventDefault();
                event.stopPropagation();
                window.open(this.href, '_blank');
            });
        }
    });
});

You could also do this in any web app that uses jQuery, of course.

(Hat-tip to Adri Kodde for posting a snippet about this.)

Comments