tags:

views:

102

answers:

5

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 params in the URL.

How can I achieve this?

Update: I am currently doing this:

<form method="get" action="/sponsor">
    <button type="submit" id="sponsorContinue">Continue</button>
</form>

but the problem with this is that in Safari and IE, 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.

+7  A: 

Put it in a <form> wherein you specify the desired target URL in the action attribute.

<form action="http://google.com"&gt;
    <input type="submit" value="Go to Google">
</form>
BalusC
Simple and nice. A fine solution. Add `display: inline` to the form to keep the button in the flow.
Pekka
in my opinion this idea is correct because it is work if user disabled the javascript
4thpage
in safari, this adds a question mark to the end of the url...is there a way to do it that doesn't add anything to the url?
Andrew
@Andrew that is probably because of the `GET` method used by default by the form everywhere but in IE. You could switch the form `method` to `POST` but that would have other consequences, namely when you use the history to browse back after clicking the button. It may be unavoidable (it will have no effect anyway).
Pekka
+1  A: 
<form>
<input TYPE="button" VALUE="Home Page"
     onclick="window.location.href='http://www.wherever.com'"&gt; 
</form>
ghoppe
@Robusto that was a snarky comment about the empty space that used to be there :) This is *not* a good solution IMO, as it won't work without JavaScript.
Pekka
@seanizer it is possible to delete comments, which BalusC and I did.
Pekka
@Pekka: yup, and also it's not even well-formed xhtml
seanizer
@Pekka: ah, me too :-)
seanizer
A: 

The only way to do this (except for BalusC's ingenious form idea!) is by adding a Javascript onclick event to the button, which is not good for accessibility.

Have you considered styling a normal link like a button? You can't achieve OS specific buttons that way, but it's still the best way IMO.

Pekka
I think he is talking about not only functionality (click) but also appearance.
Web Logic
@Web Logic yup, that's why I'm talking about styling the link to look like a button.
Pekka
+2  A: 
<a href="http://www.stackoverflow.com/"&gt;
    <button>click me</button>
</a>
RedFilter
that's cool, didn't know that one. is that valid xhtml?
seanizer
Awwww, this doesn't feel right to me. Is this valid and reliable?
Pekka
Doesn't (X)HTML-validate.
BalusC
Sorry, wrapping a button with an anchor tag is a kludge.
Robusto
Robert Harvey
@Rob: that's not my problem :) Throw it at Meta and see. However the `target` attribute is imo on the edge.
BalusC
A: 
<button onclick="location.href='http://www.example.com'"&gt;
     www.example.com</button>
Adam