|
12 | 12 |
|
13 | 13 | </p> |
14 | 14 |
|
15 | | -This plugin adds js to open outgoing links and PDFs in a new tab. |
| 15 | +This plugin adds JS code to open outgoing links and PDFs in a new tab. |
| 16 | + |
| 17 | +The automatic opening of links in a new tab is a common feature of modern websites. It is also a good practice for accessibility. However, it is not a default behavior of Markdown. This plugin adds a JavaScript code to your website that opens external links and PDFs in a new tab. |
16 | 18 |
|
17 | 19 | Look at the [demo](https://newtab.kubaandrysek.cz/). |
18 | 20 |
|
@@ -43,6 +45,61 @@ Relative link to [Relative link](./docs/RelativeLink.md) should open in the same |
43 | 45 |
|
44 | 46 | Sample PDF link to [PDF](./docs/assets/sample.pdf) should open in a new tab (pdf from [here](https://www.africau.edu/images/default/sample.pdf)). |
45 | 47 |
|
| 48 | +
|
| 49 | +# How does it work? |
| 50 | +The plugin adds a JavaScript code to your website that opens external links and PDFs in a new tab. Injection of the code is done using the `on_page_context` hook. The code is injected into the `<head>` section of the page as a `<script>` dependency of the `open_in_new_tab.js` file. The code is automatically added to all pages of your website. |
| 51 | + |
| 52 | + |
| 53 | +The function `external_new_window` checks if the link is external using the `hostname` property of the `a` element. If the link is external, the `target` attribute is set to `_blank` and the `rel` attribute is set to `noopener`. The `noopener` attribute is used to prevent the new tab from accessing the `window.opener` property and ensures that the original page will not be able to access the new tab. |
| 54 | + |
| 55 | +The same way is used to open PDF links in a new tab. |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +<details><summary>Show source code</summary> |
| 60 | +<p> |
| 61 | + |
| 62 | +Look at this source <a href="https://github.com/JakubAndrysek/mkdocs-open-in-new-tab/blob/main/open_in_new_tab/js/open_in_new_tab.js">open_in_new_tab.js</a>: |
| 63 | + |
| 64 | +```js |
| 65 | +// Description: Open external links in a new tab and PDF links in a new tab |
| 66 | +// Source: https://jekyllcodex.org/without-plugin/new-window-fix/ |
| 67 | +
|
| 68 | +//open external links in a new window |
| 69 | +function external_new_window() { |
| 70 | + for(var c = document.getElementsByTagName("a"), a = 0;a < c.length;a++) { |
| 71 | + var b = c[a]; |
| 72 | + if(b.getAttribute("href") && b.hostname !== location.hostname) { |
| 73 | + b.target = "_blank"; |
| 74 | + b.rel = "noopener"; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +//open PDF links in a new window |
| 79 | +function pdf_new_window () |
| 80 | +{ |
| 81 | + if (!document.getElementsByTagName) return false; |
| 82 | + var links = document.getElementsByTagName("a"); |
| 83 | + for (var eleLink=0; eleLink < links.length; eleLink ++) { |
| 84 | + if ((links[eleLink].href.indexOf('.pdf') !== -1)||(links[eleLink].href.indexOf('.doc') !== -1)||(links[eleLink].href.indexOf('.docx') !== -1)) { |
| 85 | + links[eleLink].onclick = |
| 86 | + function() { |
| 87 | + window.open(this.href); |
| 88 | + return false; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +
|
| 94 | +window.addEventListener("DOMContentLoaded", function() { |
| 95 | + external_new_window(); |
| 96 | + pdf_new_window(); |
| 97 | +}); |
| 98 | +``` |
| 99 | +</p> |
| 100 | +</details> |
| 101 | + |
| 102 | + |
46 | 103 | ## License |
47 | 104 |
|
48 | 105 | This project is licensed under the terms of the MIT license. |
0 commit comments