forked from whatwg/html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link-fixup.js
39 lines (34 loc) · 1.05 KB
/
link-fixup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(function () {
'use strict';
if (!(/^\/(dev|multipage)\//.test(window.location.pathname))) {
return;
}
var fragid = decodeURIComponent(window.location.hash.substr(1));
if (fragid && document.getElementById(fragid)) {
return;
}
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open('GET', '/multipage/fragment-links.json');
xhr.onload = function() {
var fragmentLinks = xhr.response;
// Handle section-foo.html links from the old old multipage version,
// and broken foo.html from the new version. Only run this for 404s.
if ((!fragid || !(fragid in fragmentLinks)) && document.title === '404 Not Found') {
var m = window.location.pathname.match(/\/(?:section-)?([\w\-]+)\.html/);
if (m) {
fragid = m[1];
}
}
if (fragid in fragmentLinks) {
var page = fragmentLinks[fragid];
if (page === '') {
page = './';
} else {
page += '.html';
}
window.location.replace(page + '#' + encodeURIComponent(fragid));
}
};
xhr.send();
})();