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>
Why not just place your button inside of a reference tag e.g
This seems to work perfectly for me and does not add any %20 tags to the link, just how you want it. I have used a link to google to demonstrate.
You could of course wrap this in a form tag but it is not necessary.
When linking another local file just put it in the same folder and add the file name as the reference. Or specify the location of the file if in is not in the same folder.
This does not add any character onto the end of the URL either, however it does have the files project path as the url before ending with the name of the file. e.g
If my project structure was...
.. denotes a folder - denotes a file while four | denote a sub directory or file in parent folder
..public
People who have answered using
<a></a>
attributes on a<button></button>
was helpful.BUT then recently, I encountered a problem when I used a link inside a
<form></form>
.The button is now regarded like/as a submit button (HTML5). I've tried working a way around, and have found this method.
Create a CSS style button like the one below:
Or create a new one here : CSS Button Generator
And then create your link with a class tag named after the CSS style you have made:
Here's a fiddle:
JS Fiddle
HTML
The plain HTML way is to put it in a
<form>
wherein you specify the desired target URL in theaction
attribute.If necessary, set CSS
display: inline;
on the form to keep it in the flow with the surrounding text. Instead of<input type="submit">
in above example, you can also use<button type="submit">
. The only difference is that the<button>
element allows children.You'd intuitively expect to be able to use
<button href="http://google.com">
analogous with the<a>
element, but unfortunately no, this attribute does not exist according to HTML specification.CSS
If CSS is allowed, simply use an
<a>
which you style to look like a button using among others theappearance
property (only Internet Explorer support is currently (July 2015) still poor).Or pick one of those many CSS libraries like Bootstrap.
JavaScript
If JavaScript is allowed, set the
window.location.href
.Another option is to create a link in the button:
Then use CSS to style the link and button, so that the link takes up the entire space within the button (so there's no miss-clicking by the user):
I have created a demo here.
If I open main.html the URL would be,
However, when I clicked the button inside main.html to change to secondary.html, the URL would be,
No special characters included at the end of the URL. I hope this helps. By the way - (%20 denotes a space in a URL its encoded and inserted in the place of them.)
Note: The localhost:0000 will obviously not be 0000 you'll have your own port number there.
Furthermore the ?_ijt=xxxxxxxxxxxxxx at the end off the main.html URL, x is determined by your own connection so obviously will not be equal to mine.
It might seem like I'm stating some really basic points but I just want to explain as best as I can. Thank you for reading and I hope this help someone at the very least. Happy programming.