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 ?
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.
I use method 3 because it's the most understandable for others (whenever you see an <a>
tag, you know it's a link) and when you are part of a team, you have to make simple things ;).
And finally I don't think it's useful and efficient to use JS simply to navigate to an other page.
Source: Stackoverflow.com