111

I am making a prototype and I want the search button to link to a sample search results page.

How do I make a button redirect to another page when it is clicked using jQuery or plain JS.

Ankur
  • 50,282
  • 110
  • 242
  • 312
  • I had asked for js because I didn't imagine that a simple HTML solution was available. Thanks it solved my purpose. – Ankur Feb 11 '10 at 08:57

15 Answers15

217

is this what you mean?

$('button selector').click(function(){
   window.location.href='the_link_to_go_to.html';
})
Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
  • 1
    According to [this stackoverflow](http://stackoverflow.com/questions/2430936/whats-the-difference-between-window-location-and-document-location-in-javascrip) question and answer, one should lean towards `window.location` and not `document.location` as it's not the canonical way. – Jujhar Singh Mar 20 '14 at 15:18
88
$('#someButton').click(function() {
    window.location.href = '/some/new/page';
    return false;
});
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 7
    why to return false ? – Francisco Corrales Morales Feb 09 '15 at 16:10
  • 2
    Without the `return false`, hitting the "Enter or Return key" might just take you to the current url rather than redirecting to the new link. Especially useful, when you have a text input control which you would like to post to a different url when you hit the enter/return key. – Faith Wins Apr 26 '16 at 17:31
  • 3
    That's not a very good explanation. The return value indicates whether to continue processing the event. By default, the event will continue to bubble up, and may trigger other actions, such as submitting a form, or going to a link, or nothing. However, you really want to "absorb" the event here. By returning false, the event processing will end here. – redreinard May 27 '16 at 00:19
  • 1
    thanks - the return false saved me - was wondering why the url woudln't change - but alas some other script must have continued processing the click. – Derrick Dennis Sep 06 '17 at 07:53
66

Without script:

<form action="where-you-want-to-go"><input type="submit"></form>

Better yet, since you are just going somewhere, present the user with the standard interface for "just going somewhere":

<a href="where-you-want-to-go">ta da</a>

Although, the context sounds like "Simulate a normal search where the user submits a form", in which case the first option is the way to go.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 14
    Who would have thought of a link? haha – meouw Feb 10 '10 at 16:36
  • +1 for the form. Since it will be a proper search form eventually you should do it like that now if possible. – DisgruntledGoat Feb 10 '10 at 16:41
  • @suhail — Three times as much code and a dependancy on JavaScript isn't good. – Quentin Jun 27 '13 at 09:51
  • @suhail — If you want to ask a question, then [search](http://stackoverflow.com/search?q=confirmation+before+link) and (if that fails) [ask a question](http://stackoverflow.com/questions/ask), don't make a comment on an answer to a tenuously related question. – Quentin Jun 27 '13 at 11:06
  • Noob here. Why is that the "input" button needs to be inside a "form" element? Couldn't input be outside of it? – ayjay May 31 '14 at 23:21
  • 1
    @ayjay — Clicking a submit button only submits a form if the submit button is associated with that form. Putting the input in it is the traditional and best supported way to do that. – Quentin Jun 01 '14 at 22:29
  • This is solution cause form resubmit problem. – mimi Sep 22 '17 at 08:00
  • 1
    @mimi — No, it doesn't. Form resubmit problems only occur when you are making a request which is not "safe" (in which case it should be a POST form … but the question was asking about simple navigation, so there is no reason not to think it is safe). Browsers only warn people about the possibility when the request may not be safe (so they won't in this case because it isn't `method="POST"`). – Quentin Sep 24 '17 at 17:16
30

In your html, you can add data attribute to your button:

<button type="submit" class="mybtn" data-target="/search.html">Search</button>

Then you can use jQuery to change the url:

$('.mybtn').on('click', function(event) {
    event.preventDefault(); 
    var url = $(this).data('target');
    location.replace(url);
});

Hope this helps

g-francesca
  • 301
  • 3
  • 3
22

With simple Javascript:

<input type="button" onclick="window.location = 'path-here';">
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
12

No need for javascript, just wrap it in a link

<a href="http://www.google.com"><button type="button">button</button></a>
Owen
  • 4,229
  • 5
  • 42
  • 50
11

You can use:

  location.href = "newpage.html"

in the button's onclick event.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
10

This should work ..

$('#buttonID').click(function(){ window.location = 'new url'});
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
7

You can use window.location

window.location="/newpage.php";

Or you can just make the form that the search button is in have a action of the page you want.

PetersenDidIt
  • 25,562
  • 3
  • 67
  • 72
5

this is the FASTEST (most readable, least complicated) way to do it, Owens works but it's not legal HTML, technically this answer is not jQuery (but since jQuery is a pre-prepared pseudocode - reinterpreted on the client platform as native JavaScript - there really is no such thing as jQuery anyway)

 <button onclick="window.location.href='http://www.google.com';">Google</button>
Mr Heelis
  • 2,370
  • 4
  • 24
  • 34
4

You can use this simple JavaScript code to make search button to link to a sample search results page. Here I have redirected to '/search' of my home page, If you want to search from Google search engine, You can use "https://www.google.com/search" in form action.

<form action="/search"> Enter your search text: 
<input type="text" id="searchtext" name="q"> &nbsp;
<input onclick="myFunction()" type="submit" value="Search It" />
</form> 
<script> function myFunction() 
{ 
var search = document.getElementById("searchtext").value; 
window.location = '/search?q='+search; 
} 
</script>
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
shuseel
  • 597
  • 6
  • 10
2

From YT 2012 code.

<button href="/signin" onclick=";window.location.href=this.getAttribute('href');return false;">Sign In</button>

DIES
  • 345
  • 5
  • 14
1

Use a link and style it like a button:

<a href="#page2" class="ui-btn ui-shadow ui-corner-all">Click this button</a>
Redwolf
  • 540
  • 4
  • 17
0

And in Rails 3 with CoffeeScript using unobtrusive JavaScript (UJS):

Add to assets/javascripts/my_controller.js.coffee:

$ ->
  $('#field_name').click ->
    window.location.href = 'new_url'

which reads: when the document.ready event has fired, add an onclick event to a DOM object whose ID is field_name which executes the javascript window.location.href='new_url';

Tom Harrison
  • 13,533
  • 3
  • 49
  • 77
0

There are a lot of questions here about client side redirect, and I can't spout off on most of them…this one is an exception.

Redirection is not supposed to come from the client…it is supposed to come from the server. If you have no control over the server, you can certainly use Javascript to choose another URL to go to, but…that is not redirection. Redirection is done with 300 status codes at the server, or by plying the META tag in HTML.