I would like to create an HTML button that acts like a link. So, when you click the button, it redirects to a page. I would like it to be as accessible as possible.
I would also like it so there aren't any extra characters, or parameters in the URL.
How can I achieve this?
Based on the answers posted so far, I am currently doing this:
<form method="get" action="/page2">
<button type="submit">Continue</button>
</form>
but the problem with this is that in Safari and Internet Explorer, it adds a question mark character to the end of the URL. I need to find a solution that doesn't add any characters to the end of the URL.
There are two other solutions to do this: Using JavaScript or styling a link to look like a button.
Using JavaScript:
<button onclick="window.location.href='/page2'">Continue</button>
But this obviously requires JavaScript, and for that reason it is less accessible to screen readers. The point of a link is to go to another page. So trying to make a button act like a link is the wrong solution. My suggestion is that you should use a link and style it to look like a button.
<a href="/link/to/page2">Continue</a>
If you want to create a button that is used for a URL anywhere, create a button class for an anchor.
In case you want the button to work as a download button
I know there have been a lot of answers submitted, but none of them seemed to really nail the problem. Here is my take at a solution:
<form method="get">
method that the OP is starting with. This works really well, but it sometimes appends a?
to the URL. The?
is the main problem.?
doesn't end up appended to the URL. It will seamlessly fallback to the<form>
method for the very small fraction of users who don't have JavaScript enabled.<form>
or<button>
even exist. I'm using jQuery in this example, because it is quick and easy, but it can be done in 'vanilla' JavaScript as well.<form>
action
attribute.JSBin Example (code snippet can't follow links)
Also you can use a button:
For example, in ASP.NET Core syntax: