Octopress Themes: Opening Links in a New Window

Octopress Themes: Opening Links in a New Window

Tags
JavaScript
jquery
octopress
mnml
Published
July 11, 2013
Author
Isaac Overacker
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:
$(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.)