Aylong (обсуждение | вклад) Нет описания правки Метка: ручная отмена |
Aylong (обсуждение | вклад) Нет описания правки Метка: отменено |
||
Строка 1: | Строка 1: | ||
function loadPage(url) { | function loadPage(url) { | ||
var xhr = new XMLHttpRequest(); | var xhr = new XMLHttpRequest(); | ||
xhr.onreadystatechange = function() { | xhr.onreadystatechange = function() { | ||
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) { | |||
document.body.innerHTML = xhr.responseText; | |||
window.history.pushState({}, '', url); | |||
updatePageTitle(); | |||
createThemeToggleButton(); | |||
} | |||
}; | }; | ||
xhr.open('GET', url, true); | xhr.open('GET', url, true); | ||
Строка 20: | Строка 16: | ||
var link = event.target.closest('a'); | var link = event.target.closest('a'); | ||
if (link && link.href && link.href.indexOf(window.location.origin) !== -1) { | if (link && link.href && link.href.indexOf(window.location.origin) !== -1) { | ||
event.preventDefault(); | |||
loadPage(link.href); | |||
} | } | ||
}); | }); | ||
window.addEventListener('popstate', function(event) { | window.addEventListener('popstate', function(event) { | ||
loadPage(window.location.href); | loadPage(window.location.href); | ||
}); | }); | ||
function updatePageTitle() { | function updatePageTitle() { | ||
document.title = document.querySelector('h1').innerText; | |||
} | } | ||
function createThemeToggleButton() { | function createThemeToggleButton() { | ||
var container = document.getElementById("p-personal"); | var container = document.getElementById("p-personal"); | ||
if (container) { | if (container) { | ||
var checkbox = document.createElement("input"); | |||
checkbox.type = "checkbox"; | |||
checkbox.id = "theme-toggle"; | |||
var label = document.createElement("label"); | |||
label.htmlFor = "theme-toggle"; | |||
label.id = "theme-button"; | |||
container.parentNode.insertBefore(checkbox, container); | |||
container.parentNode.insertBefore(label, container); | |||
} | } | ||
var isLightTheme = localStorage.getItem("isLightTheme"); | var isLightTheme = localStorage.getItem("isLightTheme"); | ||
document.documentElement.classList.toggle('light', isLightTheme === "true"); | |||
$("#theme-toggle").change( | $("#theme-toggle").change(toggleTheme); | ||
} | } | ||
Версия от 21:26, 4 мая 2024
function loadPage(url) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) { document.body.innerHTML = xhr.responseText; window.history.pushState({}, '', url); updatePageTitle(); createThemeToggleButton(); } }; xhr.open('GET', url, true); xhr.send(); } document.addEventListener('click', function(event) { var link = event.target.closest('a'); if (link && link.href && link.href.indexOf(window.location.origin) !== -1) { event.preventDefault(); loadPage(link.href); } }); window.addEventListener('popstate', function(event) { loadPage(window.location.href); }); function updatePageTitle() { document.title = document.querySelector('h1').innerText; } function createThemeToggleButton() { var container = document.getElementById("p-personal"); if (container) { var checkbox = document.createElement("input"); checkbox.type = "checkbox"; checkbox.id = "theme-toggle"; var label = document.createElement("label"); label.htmlFor = "theme-toggle"; label.id = "theme-button"; container.parentNode.insertBefore(checkbox, container); container.parentNode.insertBefore(label, container); } var isLightTheme = localStorage.getItem("isLightTheme"); document.documentElement.classList.toggle('light', isLightTheme === "true"); $("#theme-toggle").change(toggleTheme); } function toggleTheme() { var isLightTheme = document.documentElement.classList.toggle('light'); localStorage.setItem("isLightTheme", isLightTheme); } createThemeToggleButton();