What is the correct way to use JavaScript to dynamically change the URL after "?redirect=" to match the current visiting URL in Webflow?

TL;DR
  • Add custom JavaScript in Webflow to target links with a redirect= parameter.  
  • Use window.location.href and encodeURIComponent(...) to update each link’s redirect= value with the current page URL.

You want to use JavaScript to dynamically update a query parameter like ?redirect= so that it reflects the current page's full URL in Webflow.

1. Understand the Use Case

  • You might have a link like example.com/login?redirect= and want the value after redirect= to dynamically match the current page's URL, e.g., example.com/pricing.
  • This is common for custom login flows using services like Memberstack, Firebase Auth, or OAuth.

2. Insert Custom Code to Update the redirect= Parameter

  • Add custom JavaScript to your Webflow project using the Page Settings > Before </body> tag section or in an Embed element.
  • Use this JavaScript to find links with a redirect parameter and set its value to the current location.

Example JS logic (description only):

  • Select all links that include ?redirect= or &redirect=.
  • For each, replace the value after redirect= with window.location.href.

3. Recommended JavaScript Logic

Use this description to implement in your custom code section:

  • Get the current full URL using window.location.href.
  • Use URLSearchParams or .split() methods to manipulate existing links.
  • Update the href attributes of your target links, injecting the current URL after redirect=.

For example:

  • Select anchor tags with a specific class or attribute.
  • Update their href so that redirect= is followed by encodeURIComponent(window.location.href).

4. Example Usage

If you have a login link like:

  • https://example.com/login?redirect=

You change it dynamically via JavaScript so that it becomes:

  • https://example.com/login?redirect=https%3A%2F%2Fexample.com%2Fpricing

Assuming the current page is /pricing.

Summary

To dynamically insert the current URL into a ?redirect= parameter in Webflow:

  • Use custom JavaScript added to the page’s body or an Embed element.
  • Read window.location.href to get the current page URL.
  • Update each link's redirect= value using encodeURIComponent(...) to ensure the full URL is safely encoded.
  • This ensures users are redirected back to the original page after login or other processes.
Rate this answer

Other Webflow Questions