Ads by ProfitSence
Close

How to Redirect to Another Page in JavaScript on button click

Last Updated on Sunday 20th Nov 2022
Redirect to Another Page in JavaScript

On Button Click ReDirect to Another Page

Sometimes You may want to use a button to link to another page or website.

Add an onclick event to the button.

			
					<button onclick="location.href = 'example.com';" id="myButton">
Button
</button>

			
	
			
					<button id="myButton">Home</button>

			
	
			
					document.getElementById("myButton").onclick = function () {
     location.href = "example.com";
};

			
	

Name A function to HTML Element

HTML Button onClick Redirect

			
					<button name="redirect" onClick="redirect()">

			
	
			
					function redirect() {
   let url = "http://example.com";
   window.location(url);
}

			
	

You could change the action attribute of the form on Click the button

			
					myFun(){
   $('form').attr('action','new path');
}

			
	

References

on click redirect to another page
on button click redirect to another page in javascript
how to redirect to another page in html
how to redirect javascript
how to redirect in javascript
how to redirect with javascript
onclick redirect to another page
button redirect to another page
onclick redirect
javascript redirect