|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// detect native/existing fragmention support |
| 4 | +if (!('fragmention' in window.location)) (function () { |
| 5 | + // populate fragmention |
| 6 | + location.fragmention = location.fragmention || ''; |
| 7 | + |
| 8 | + // return first element in scope containing case-sensitive text |
| 9 | + function getElementByText(scope, text) { |
| 10 | + // iterate descendants of scope |
| 11 | + for (var all = scope.childNodes, index = 0, element; (element = all[index]); ++index) { |
| 12 | + // conditionally return element containing visible, whitespace-insensitive, case-sensitive text (a match) |
| 13 | + if (element.nodeType == 1 && (element.innerText || element.textContent || '').replace(/\s+/g, ' ').indexOf(text) !== -1) { |
| 14 | + return getElementByText(element, text); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + // return scope (no match) |
| 19 | + return scope; |
| 20 | + } |
| 21 | + |
| 22 | + // on dom ready or hash change |
| 23 | + function onHashChange() { |
| 24 | + // set location fragmention as uri-decoded text (from href, as hash may be decoded) |
| 25 | + location.fragmention = decodeURIComponent((location.href.match(/#(#|%23)(.+)/) || [0,0,''])[2].replace(/\+/g, ' ')); |
| 26 | + |
| 27 | + // conditionally remove stashed element fragmention attribute |
| 28 | + if (element) { |
| 29 | + element.removeAttribute('fragmention'); |
| 30 | + |
| 31 | + // DEPRECATED: trigger style in IE8 |
| 32 | + if (element.runtimeStyle) { |
| 33 | + element.runtimeStyle.windows = element.runtimeStyle.windows; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // if fragmention exists |
| 38 | + if (location.fragmention) { |
| 39 | + // get element containing text (or return document) |
| 40 | + element = getElementByText(document, location.fragmention); |
| 41 | + |
| 42 | + // if element found |
| 43 | + if (element !== document) { |
| 44 | + // scroll to element |
| 45 | + element.scrollIntoView(); |
| 46 | + |
| 47 | + // set fragmention attribute |
| 48 | + element.setAttribute('fragmention', ''); |
| 49 | + |
| 50 | + // DEPRECATED: trigger style in IE8 |
| 51 | + if (element.runtimeStyle) { |
| 52 | + element.runtimeStyle.windows = element.runtimeStyle.windows; |
| 53 | + } |
| 54 | + } |
| 55 | + // otherwise clear stashed element |
| 56 | + else { |
| 57 | + element = null; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // set stashed element |
| 63 | + var element; |
| 64 | + |
| 65 | + // add listeners |
| 66 | + if ('addEventListener' in window) { |
| 67 | + window.addEventListener('hashchange', onHashChange); |
| 68 | + document.addEventListener('DOMContentLoaded', onHashChange); |
| 69 | + } |
| 70 | + // DEPRECATED: otherwise use old IE attachEvent |
| 71 | + else { |
| 72 | + window.attachEvent('onhashchange', onHashChange); |
| 73 | + document.attachEvent('onreadystatechange', function () { |
| 74 | + if (document.readyState[0] === 'c') { |
| 75 | + onHashChange(); |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | +})(); |
0 commit comments