Open a link in a new window in reStructuredText

Viewed 14716

I want to open a link in a new window using reStucturedText. Is this possible?

This opens link in the same window:

You can `check your location here. <http://geoiptool.com>`_
4 Answers

If you don't want to modify the theme, you can do what Ivonet did but with a custom.js file in the _static/js folder, then adding it to the conf.py like this:

html_js_files = [
    'js/custom.js'
]

Leave out the html tags in _static/js/custom.js if you do this:

$(document).ready(function () {
   $('a[href^="http://"], a[href^="https://"]').not('a[class*=internal]').attr('target', '_blank');
});

This will also work if you'd like a shorter version.

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

I agree completely with the accepted answer, especially with the part where reStructuredText is not responsible for how a link behaves.

I still want it though so it should be solved in the theme. As I want all my external links to open in a new tab it becomes very cumbersome to do it as described above.

In my case I use a third party theme (sphinx_rtd_theme) and I put the following script near the end of the layout.html:

  <script type="text/javascript">
    <!-- Adds target=_blank to external links -->

    $(document).ready(function () {
      $('a[href^="http://"], a[href^="https://"]').not('a[class*=internal]').attr('target', '_blank');
    });
  </script>

It seems to do the job just fine. Hope it helps.

Related