This is a quick post showing how to use JavaScript to make links to external websites open in a new window (or tab) instead of in the current window. This is useful for Jekyll blogs because the Markdown converters don’t do this for you. I included two versions: one that uses straight JavaScript, and one that requires jQuery but is shorter.
Both versions work basically the same way: grab all anchor tags <a href="#"> that are linking to somewhere other than your development environment or a page on your site and then attribute target="_blank" to those tags. Because this is JavaScript, users with JavaScript disabled will still experience the old behavior, but otherwise won’t be adversely affected.
I have created a file called external-links-new-window.html
inside _includes directory and referred it from _layouts/default.html as {% include external-links-new-window.html %}.