5

When a visitor clicks a button element, how can I change the URL?

Example:

<form id="search">
  <input type="text" />
  <button onclick="javascript:alert('hi');">Alert Hi</button>
  <button onclick="javascript:window.location.href='http://www.google.com/'">Go to Google</button>
</form>

(Or see JSFiddle.)

For a quick demo page, I just need to send the visitor to a new URL when they click a button. I can execute JavaScript onclick (in the Alert Hi button), and the URL changes if I execute window.location.href='http://www.google.com/' from Firebug's console. What do I need to do to change the URL on a button click?

KatieK
  • 13,586
  • 17
  • 76
  • 90
  • Your buttons submit the form. Also since you have a fiddle you should know that Google won't load in a frame because of `X-frame-options` – Musa Nov 16 '12 at 23:25
  • So an anchor tag just won't suffice? – Shmiddty Nov 16 '12 at 23:27
  • @Shmiddty - I know! In this case, the button is nicely styled, but the page is in a half built state as I'm waiting on some specs from a stakeholder, so we're faking it. – KatieK Nov 16 '12 at 23:33
  • @KatieK See the newest updates to my answer about making it work without JS – Ruan Mendes Nov 16 '12 at 23:57
  • @KatieK I love the windows shortcuts tips on your webpage, I didn't know about shift+right click for extra options – Ruan Mendes Dec 04 '12 at 21:31
  • @JuanMendes Cool, glad to hear it was helpful FWIW, I have an RSS feed at http://www.pewpewlaser.com/pplb.rss. – KatieK Dec 04 '12 at 21:36

2 Answers2

19

The problem is that the form is being submitted when you click on the button. Add type="button" so it doesn't submit the form. You also don't need javascript in the onclick attribute. See How to prevent buttons from submitting forms

Example http://jsfiddle.net/E7UEe/1/ Notice that it won't go to google.com because jsfiddle has disallowed it. Notice the error message Refused to display document because display forbidden by X-Frame-Options.

<form id="search">
  <input type="text" />
  <button onclick="javascript:alert('hi');">Alert Hi</button>
  <button type="button" onclick="window.location.href='http://www.google.com/'">Go to Google</button>
</form>​

An alternative that works without JS is the appearance:button CSS directive http://jsfiddle.net/d6gWA/

<a class="btn"> Link looking like button</a>​
.btn {
    appearance: button;
    -moz-appearance: button;
    -webkit-appearance: button;
    -ms-appearance: button;
}​

Or you could always put the button in a link http://jsfiddle.net/d6gWA/2/

<a class="btn" href="http://www.google.com" target="_blank"><button type="button">Link Button</button></a>​
Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Is a button assumed to be a submit button? – KatieK Nov 16 '12 at 23:29
  • 3
    Yes http://dev.w3.org/html5/markup/button.html `A button element with no type attribute specified represents the same thing as a button element with its type attribute set to "submit".` – Ruan Mendes Nov 16 '12 at 23:35
-1
 <button onclick="javascript:window.location='http://www.google.com/'">Go to Google</button>

just remove your .href and it sould work