Explain Codes LogoExplain Codes Logo

How can I make a button redirect my page to another page?

javascript
event-listeners
redirect-techniques
web-development
Anton ShumikhinbyAnton Shumikhin·Feb 10, 2025
TLDR

For a swift page redirect, use:

window.location = 'https://www.example.com';

To associate this with a button click:

<button onclick="window.location = 'https://www.example.com';">Click to go</button>

Now, clicking the button instantly takes you to the designated URL.

Understanding button redirects in depth

Redirecting users to another webpage upon clicking a button is not a Herculean task. With slight variations in the approach, the code can be further optimized for user experience and maintainability.

Move to event listeners

Say no to inline JavaScript. Use event listeners for improved code separation and maintenance:

document.getElementById('redirectButton').addEventListener('click', function() { window.location.href = 'https://www.example.com'; });

Using ID to target button

The button can be conveniently targeted using an ID attribute:

<button id="redirectButton">Go to Example</button>

The matter of URL formats

Don't just take your URL to the park, take it to the full-fledged party! Remember to include the protocol (http:// or https://). This small detail could be the difference between a working and a failing redirect.

Security and sanity checks

Redirecting can be a slippery slope, especially when the target URL is inputted by the user. One wrong move and it could lead to security threats. Therefore, validate this input like a bouncer at an exclusive club.

When gearing up for a page redirect, double-check the URL to avoid adding any erroneous characters or parameters. And don't forget your safety belt - thorough testing!

Further redirection techniques

Redirecting using forms

Sometimes, a form completion should lead to a redirect. Handling this in the submit event is a solid move:

document.querySelector('form').onsubmit = function(e) { e.preventDefault(); // says "Stop! Hammertime" to the default form submission window.location.href = 'https://www.example.com'; };

Redirections driven by data attributes

Truly play the puppet master by defining target URLs in data attributes:

<button id="dynamicRedirectButton" data-url="https://www.example.com">Visit Example</button>
document.getElementById('dynamicRedirectButton').addEventListener('click', function() { window.location.href = this.getAttribute('data-url'); // The button obeys thy command });

This trick allows you to change the URL destination like you change your clothes: without touching the JavaScript.

Handling Single Page Applications (SPA)

In the world of Single Page Applications (SPA), you would rely on native routing methods instead of window.location.href.

// React, for instance <Redirect to='/new-page' />

A react-router <Redirect /> respects the SPA principles and won't refresh the whole page like a melodramatic actor. It keeps the performance at its peak.