How to redirect after click on button?

Member

by adan , in category: Third Party Scripts , 2 months ago

How to redirect after click on button?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by addison , 2 months ago

@adan 

You can redirect the user to another page after they click on a button by using JavaScript. Here is an example code for redirecting the user to a different URL after clicking on a button:


HTML:

1
<button id="redirectButton">Click me to redirect</button>


JavaScript:

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


In this code, we use the addEventListener method to listen for a click event on the button with the id "redirectButton". When the button is clicked, we use window.location.href to redirect the user to the specified URL (in this example, "https://www.example.com"). You can replace this URL with the desired destination for the redirection.