Using jQuery to Open External Links in New Windows1
Creating (static) webpages using markdown is every convenient, for example
However, left clicking a link by default opens it in the same window. If there are only a few links, one may use html codes to open a link in a new window, for example
When there are many links it will be a little bit painful to create such links using html in a markdown file. I expected a few lines of CSS code will do the job. Indee, CSS3 does have a target-new
property, which is unfortunately not supported by any major browsers yet. Another solution I found is to use jQuery.
- We first need to add jQuery to the inside of the
<head>
section.
- To open links in new windows, we add an attribute
target="_blank"
(to all links ).
- We may also add a message after the link to be open in a new window.
- We may let the function apply to specific link using keyword.
Using Kramdown
.
Kramdown supports it in the following way:
-
The jQuery solution was inspired by the post Make External Links Open In New Windows of Charles Wyke-Smith and the answer of gion_13 to this question jQuery add target=“_blank” for outgoing link on stackoverflow. ↩︎