To implement a back button with conditional behavior in Webflow using JavaScript, you’ll check the user's referrer and decide whether to go back or redirect.
back-button
) to target it with JavaScript.tag placement.
<script> document.addEventListener("DOMContentLoaded", function () { const sameDomain = location.hostname; const referrerDomain = document.referrer ? new URL(document.referrer).hostname : null; const backBtn = document.getElementById('back-button'); // Adjust to your button's ID if (backBtn) { backBtn.addEventListener('click', function (e) { e.preventDefault(); if (referrerDomain && referrerDomain === sameDomain) { window.history.back(); } else { window.location.href = "/default-page"; // Update with your fallback URL } }); } });</script>
/default-page
with the actual path to your fallback page.back-button
).document.referrer
is available.To create a conditional back button in Webflow, use JavaScript to check if document.referrer
matches your domain. If so, go back in history; otherwise, redirect to a default page. This ensures users return consistently whether they came from internal or external pages.