From: Chris Fulljames Date: Tue, 18 Feb 2025 12:26:09 +0000 (-0500) Subject: Fix browser URLs when loading ajax pages X-Git-Url: https://littlesong.place/gitweb/?a=commitdiff_plain;h=3d8fe359578755d53bb8bddd7a2e70792e8bca61;p=littlesongplace.git Fix browser URLs when loading ajax pages --- diff --git a/templates/base.html b/templates/base.html index 387d8a2..26390c1 100644 --- a/templates/base.html +++ b/templates/base.html @@ -17,10 +17,7 @@ } event.preventDefault(); - // Update URL in browser window - //window.history.pushState({}, "", event.target.href); - - // Make AJAX request + // Add AJAX type specifier to URL params var url = new URL(event.target.href); url.searchParams.set("request-type", "ajax") @@ -29,12 +26,18 @@ }); async function getPageAjax(url) { fetch(url, {redirect: "follow"}).then(async (response) => { - console.log(response); - window.history.pushState({}, "", response.url); + // Update URL in browser window, minus request-type field + var url = new URL(response.url); + url.searchParams.delete("request-type"); + window.history.pushState({}, "", url); + + // Get page content from JSON response var data = await response.json(); console.log(data); document.getElementById("main").innerHTML = data.body; document.title = data.title; + + // Trigger event to signal new page has loaded var event = new Event("DOMContentLoaded"); document.dispatchEvent(event); }); diff --git a/templates/base.json b/templates/base.json new file mode 100644 index 0000000..03cdf69 --- /dev/null +++ b/templates/base.json @@ -0,0 +1,4 @@ +{ + "title": {{ dumps(self.title()) | safe }}, + "body": {{ dumps(self.body()) | safe }} +} diff --git a/todo.txt b/todo.txt index c61f7bf..3606dc4 100644 --- a/todo.txt +++ b/todo.txt @@ -5,6 +5,8 @@ NOW - Back (history stack) - Other on-site links (songs, profiles, tags, etc.) - Update page colors + - PFP on profile when missing PFP + - Signout doesn't refresh navbar (should show sign in link) - Break up main.py, test_offline.py - Pinned profile playlists