Make sure all browsers can scroll to permalinks
This commit is contained in:
parent
81a5171c23
commit
64dd87972f
26
js/script.js
26
js/script.js
|
@ -5,15 +5,39 @@ function hasElementNodes (parent) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener('load', function () {
|
/*
|
||||||
|
* On some browsers that do not support `disable-output-escaping` in XSLTs,
|
||||||
|
* the XSLT that renders RSRSSS may be outputting HTML tags that are still escaped.
|
||||||
|
* If the browser supports JavaScript, this function offers a last chance:
|
||||||
|
* if no post has any HTML tags, only text, then it assumes escaping failed
|
||||||
|
* and does the unescaping through a hidden textarea.
|
||||||
|
*/
|
||||||
|
function forceUnescape() {
|
||||||
var escaped = document.getElementsByClassName('unescape');
|
var escaped = document.getElementsByClassName('unescape');
|
||||||
for (var i = 0; i < escaped.length; i++) {
|
for (var i = 0; i < escaped.length; i++) {
|
||||||
if (hasElementNodes(escaped[i])) return;
|
if (hasElementNodes(escaped[i])) return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var textarea = document.createElement('textarea');
|
var textarea = document.createElement('textarea');
|
||||||
for (var i = escaped.length - 1; i >= 0; i--) {
|
for (var i = escaped.length - 1; i >= 0; i--) {
|
||||||
textarea.innerHTML = escaped[i].innerHTML;
|
textarea.innerHTML = escaped[i].innerHTML;
|
||||||
escaped[i].outerHTML = textarea.value;
|
escaped[i].outerHTML = textarea.value;
|
||||||
}
|
}
|
||||||
textarea.innerHTML = '';
|
textarea.innerHTML = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('load', function () {
|
||||||
|
forceUnescape();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Unescaping is very likely to mess with the browser's ability to scroll to
|
||||||
|
* an element by ID through hashes in URLs.
|
||||||
|
* Some browsers might also not support scrolling to an element after processing an XSLT.
|
||||||
|
* Just in case, if there is a URL hash, we try to find an element and scroll to it.
|
||||||
|
* If the window was already scrolled, do nothing.
|
||||||
|
*/
|
||||||
|
if (window.location.hash && window.location.hash.slice(1).length && window.scrollY === 0) {
|
||||||
|
var element = document.getElementById(window.location.hash.slice(1))
|
||||||
|
if (element) element.scrollIntoView()
|
||||||
|
}
|
||||||
});
|
});
|
Loading…
Reference in New Issue