There are multiple ways to create a HTML button to navigate to other page.
Method 1
<button id="btn_click">Click Me</button>
<script>
$('#btn_click').on('click', function() { window.location = 'http://www.google.com'; });
</script>
Method 2
<button onclick="window.location='http://www.google.com'">Click Me</button>
Method 3
<a class="click-me" href="http://www.google.com">Click me</a>
<style>
.clickMe {
-moz-appearance: button;
-ms-appearance: button;
-o-appearance: button;
-webkit-appearance: button;
appearance: button;
text-decoration: none;
color: #000;
padding: 0.2em 0.4em;
}
</style>
appearance
is a CSS3 property )Method 4
<form action="http://www.google.com">
<button>Click Me</button>
</form>
<form>
tag. Does not work if there is another submit buttonProgrammers, which approaches is the most efficient (and hence used widely), and why?
Note: can we make this as community wiki ?
You can use form tags in HTML for redirecting. The action and method attribute can be used for redirecting to another page. Anchor tags can also be used for redirecting. You can specify the URL in the href attribute of anchor tags in HTML.
To make button type submit redirect to another page, You can use HTML Anchor tags <a>. Where you need to write your HTML Button [button type submit] between these HTML Anchor Tag's starting <a> and Closing </a> Tags. or you can use HTML Form Tags to do the Same thing.
To create a button link to another page in HTML,just add tag and wrap it around the simple Html button. Inside a tag simply use href=“” attribute to give the path of the desired page.
I make a link. A link is a link. A link navigates to another page. That is what links are for and everybody understands that. So Method 3 is the only correct method in my book.
I wouldn't want my link to look like a button at all, and when I do, I still think functionality is more important than looks.
Buttons are less accessible, not only due to the need of Javascript, but also because tools for the visually impaired may not understand this Javascript enhanced button well.
Method 4 would work as well, but it is more a trick than a real functionality. You abuse a form to post 'nothing' to this other page. It's not clean.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With