In JavaScript, What Is The Method To Make A Button Navigate To A Different Page?

To make a button redirect your page to another page using JavaScript, you can use the window.location object. Here’s an example of how you can achieve this:


<button>Click me to redirect</button>


const redirectButton = document.getElementById('redirectButton');

redirectButton.addEventListener('click', () => {
  // Replace '' with the URL of the page you want to redirect to
  window.location.href = '';

In this example, we add a click event listener to the button using addEventListener. When the button is clicked, the code inside the event listener function is executed. The window.location.href property is then set to the desired URL, causing the page to redirect to that new URL.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!