MediaWiki:Common.js: Difference between revisions
From Descendants of Darkness Wiki
Created page with "→Any JavaScript here will be loaded for all users on every page load.: // Save the scroll position before the page unloads window.addEventListener('beforeunload', function() { localStorage.setItem('sidebarScrollPosition', document.getElementById('mw-navigation').scrollTop); }); // Restore the scroll position after the new page loads window.addEventListener('load', function() { var scrollPosition = localStorage.getItem('sidebarScrollPosition'); if (scro..." |
No edit summary |
||
| Line 1: | Line 1: | ||
/ | (function () { | ||
var sidebarId = 'mw-navigation'; // Replace with the ID of your sidebar element | |||
// Save the scroll position before the page unloads | |||
window.addEventListener('beforeunload', function () { | |||
localStorage.setItem('sidebarScrollPosition', document.getElementById(sidebarId).scrollTop); | |||
}); | |||
// Restore the scroll position after the new page loads | |||
window.addEventListener('DOMContentLoaded', function () { | |||
var scrollPosition = localStorage.getItem('sidebarScrollPosition'); | |||
if (scrollPosition !== null) { | |||
document.getElementById(sidebarId).scrollTop = parseInt(scrollPosition); | |||
// Restore the scroll position after the new page loads | } | ||
window.addEventListener(' | }); | ||
}); | |||
// Save the scroll position when clicking on sidebar links | // Save the scroll position when clicking on sidebar links | ||
document.querySelectorAll('# | document.querySelectorAll('#' + sidebarId + ' a').forEach(function (link) { | ||
link.addEventListener('click', function () { | |||
var scrollPosition = document.getElementById(sidebarId).scrollTop; | |||
localStorage.setItem('sidebarScrollPosition', scrollPosition); | |||
}); | |||
}); | }); | ||
}); | })(); | ||
Revision as of 19:38, 6 April 2024
(function () {
var sidebarId = 'mw-navigation'; // Replace with the ID of your sidebar element
// Save the scroll position before the page unloads
window.addEventListener('beforeunload', function () {
localStorage.setItem('sidebarScrollPosition', document.getElementById(sidebarId).scrollTop);
});
// Restore the scroll position after the new page loads
window.addEventListener('DOMContentLoaded', function () {
var scrollPosition = localStorage.getItem('sidebarScrollPosition');
if (scrollPosition !== null) {
document.getElementById(sidebarId).scrollTop = parseInt(scrollPosition);
}
});
// Save the scroll position when clicking on sidebar links
document.querySelectorAll('#' + sidebarId + ' a').forEach(function (link) {
link.addEventListener('click', function () {
var scrollPosition = document.getElementById(sidebarId).scrollTop;
localStorage.setItem('sidebarScrollPosition', scrollPosition);
});
});
})();