|
26 | 26 | </footer>
|
27 | 27 |
|
28 | 28 | <script type="text/javascript">
|
29 |
| - var links = document.links; |
30 |
| - for (var i = 0, linksLength = links.length; i < linksLength; i++) { |
31 |
| - if (links[i].hostname != window.location.hostname) { |
32 |
| - links[i].target = '_blank'; |
33 |
| - } |
| 29 | + function ensureExternalLinksOpenInNewTab() { |
| 30 | + const links = document.links; |
| 31 | + for (let i = 0, linksLength = links.length; i < linksLength; i++) { |
| 32 | + if (links[i].hostname != window.location.hostname) { |
| 33 | + links[i].target = '_blank'; |
| 34 | + } |
| 35 | + } |
34 | 36 | }
|
| 37 | + |
| 38 | + function scrollToAnchorNestedInDetailsIfNeeded() { |
| 39 | + function scrollToElement() { |
| 40 | + const id = window.location.hash.substr(1); |
| 41 | + if (id === '') { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const element = document.getElementById(id); |
| 46 | + if (!element) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + const details = document.getElementsByTagName('details'); |
| 51 | + for (const element of details) { |
| 52 | + element.open = false; |
| 53 | + } |
| 54 | + |
| 55 | + let temp = element; |
| 56 | + while (temp && temp.tagName) { |
| 57 | + if (temp.tagName.toLowerCase() === 'details') { |
| 58 | + temp.open = true; |
| 59 | + } |
| 60 | + |
| 61 | + temp = temp.parentNode; |
| 62 | + } |
| 63 | + |
| 64 | + element.scrollIntoView(); |
| 65 | + } |
| 66 | + |
| 67 | + function onReady(callback) { |
| 68 | + if (document.readyState !== 'loading') { |
| 69 | + callback(); |
| 70 | + } else if (document.addEventListener) { |
| 71 | + document.addEventListener('DOMContentLoaded', callback); |
| 72 | + } else { |
| 73 | + document.attachEvent('onreadystatechange', function() { |
| 74 | + if (document.readyState === 'complete') { |
| 75 | + callback(); |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + onReady(function () { |
| 82 | + scrollToElement(); |
| 83 | + window.addEventListener('hashchange', scrollToElement); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + ensureExternalLinksOpenInNewTab(); |
| 88 | + scrollToAnchorNestedInDetailsIfNeeded(); |
35 | 89 | </script>
|
0 commit comments