To dynamically change the value of ?redirect=
in a URL on a Webflow site using JavaScript, you'll need to detect the current page URL and then update relevant links on the page.
Your aim is to dynamically replace or update the ?redirect=
parameter in certain links to reflect the current page URL.
window.location.href
to get the full URL of the current page.https://yourwebsite.com/page
, this will return that full string.?redirect=
parameter and update the parameter value.Here’s how to do it using standard JS (add this in Page Settings → Before
tag within Webflow):
.querySelectorAll("[data-redirect-link]")
using a custom attribute like data-redirect-link
).?redirect=
values or append it if it doesn't exist.Example:
document.addEventListener("DOMContentLoaded", function () { const currentUrl = window.location.href; const links = document.querySelectorAll("a[data-redirect-link]"); // Add this custom attribute to your target links links.forEach(link => { const baseUrl = link.href.split("?redirect=")[0]; link.href = `${baseUrl}?redirect=${encodeURIComponent(currentUrl)}`; });});
data-redirect-link
as a custom attribute to any link that should have this behavior.To dynamically inject the current page URL into ?redirect=
parameters in Webflow:
window.location.href
to get the active URL.data-redirect-link
.?redirect=
parameter accordingly during page load.