views:

73682

answers:

8

How can I redirect the user from one page to another using jQuery?

+33  A: 

You don't need jQuery to do just that:

window.location = "http://www.page-2.com";
Ionuț G. Stan
note that you don't need jquery
Matt Briggs
and this is why i tell people to learn javascript before learning a library.
geowa4
@George IV, I totally agree with that.
Ionuț G. Stan
I somewhat agree with George, but actually since I've been learning jQuery it has actually helped me learn javascript, but it is important to know that jQuery is just that abstraction/tool that simplifies javascript for you.
Jon Erickson
You mean to tell me there's an underlying technology to jQuery this whole time? Are you ****ing with me?
TheTXI
+2  A: 

It would help if you were a little more descriptive in what you are trying to do. If you are trying to generate paged data, there are some options in how you do this. You can generate separate links for each page that you want to be able to get directly to.

<a href='/path-to-page?page=1' class='pager-link'>1</a>
<a href='/path-to-page?page=2' class='pager-link'>2</a>
<span class='pager-link current-page'>3</a>
...

Note that the current page in the example is handled differently in the code and with CSS.

If you want the paged data to be changed via AJAX, this is where jQuery would come in. What you would do is add a click handler to each of the anchor tags corresponding to a different page. This click handler would invoke some jQuery code that goes and fetches the next page via AJAX and updates the table with the new data. The example below assumes that you have a web service that returns the new page data.

$(document).ready( function() {
    $('a.pager-link').click( function() {
        var page = $(this).attr('href').split(/\?/)[1];
        $.ajax({
            type: 'POST',
            url: '/path-to-service',
            data: page,
            success: function(content) {
               $('#myTable').html(content);  // replace
            }
        });
        return false; // to stop link
    });
});
tvanfosson
+137  A: 

Not jQuery specific, but window.location.replace(...) will accomplish what you want. It is better than using window.location.href = if you don't want the originating page to show up in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.

window.location.replace("http://stackoverflow.com");
Ryan McGeary
A: 

easy

$(document).ready(function(){ $("#header").click(function(){ top.location="index.php"}); });

kuto
+1  A: 

var url = 'asdf.html'; window.location.href = url; hf

kuko
+33  A: 

Simply do :

var url = "http://stackoverflow.com";    
$(location).attr('href',url);
Boris Guéry
That's what I was looking for. Thanks.
lucha libre
That's terrible. Seriously? Do it the normal JavaScript way.
palswim
A: 
window.location = "http://www.page-2.com"; 

does not work in IE.

Deepfreezed
A: 

hi

this works for every browser:

window.location.href='your_url';

good luck!

Fred