How can I redirect the user from one page to another using jQuery?
jQuery is not necessary, and It is better than using For example:
|
|||||||||||||||||||||
|
WARNING: This answer has been provided as a possible solution. Although, obviously, the pure JavaScript approach is the best one, as this requires jQuery.
|
|||||||||||||||||||||
|
You don't need jQuery to do just that:
|
|||||||||||||||||||||
|
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.
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.
|
|||
|
This works for every browser:
Good luck! |
|||||||||
|
All of these answers are correct, but I'll post this for those who might run into the same strange issue that I did. I was having an issue with In IE8 and lower, location.href (or any & all variations location - will lose referrer), which for secure sites is important to maintain, because testing for it (url pasting/ session / etc) can be helpful in telling whether a request is legitimate. (Note :: there are also ways to work-around / spoof these referrers, as noted by droop's link in the comments) My cross-browser fix is this simple function. Assuming you, of course, are worried about losing Usage:
|
|||||||||||||||||||||
|
I also think that location.replace(url) is the best way, but if you want to notify the search engines about you redirection (they don't analyze javascript to see the redirection) you should add the rel="canonical" meta tag to your website. Adding a noscript section with a html refresh meta tag in it, is also a good solution. I suggest you to use this javascript redirection tool to create redirections. It also have an IE support to pass the http referrer. A sample code without delay look like this:
|
|||||
|
|
||||
|
You can do that without jQuery as:
And if you want only jQuery then you can do it like :
|
|||
|
But if some one wants to redirect back to home page then he may use the following snippet.
It would be helpful if you have three different environments as development, staging, and production. You can explore this window or window.location object by just putting these words in Chrome Console or Firebug's Console. |
|||||||||
|
This version works well with jQuery 1.6.2. |
||||
|
JavaScript provides you many methods to retrieve and change the current URL which is displayed in browser's address bar. All these methods uses the Location object, which is a property of the Window object. You can create a new Location object that has the current URL as follows..
Basic Structure of a URL
With these Location object properties you can access all of these URL components
Now If you want to change a page or redirect the user to some other page you can use the You can use the href property of the Location object.
Location Object also have these three methods
You can use assign() and replace methods also to redirect to other pages like these
How assign() and replace() differs -- The difference between replace() method and assign() method(), is that replace() removes the URL of the current document from the document history, means it is not possible to use the "back" button to navigate back to the original document. So Use the assign() method if you want to load a new document, andwant to give the option to navigate back to the original document. You can change the location object href property using jQuery also like this
And hence you can redirect the user to some other url. |
||||
|
On your click function just add
|
||||
|
So, the question is how to make a redirect page, and not how to redirect to a website? You only need to use Javascript for this. Here is some tiny code that will create a dynamic redirect page.
So say you just put this snippet into a
And if you go to that link it will automatically redirect you to google.com. And that's how you make a Simple redirect page with javasctript Edit: There is also 1 thing to note, I have added Example:
So, if this suits your needs then everything should be find, if you want to include the redirect page in the browser history replace this
with
|
||||
|
jQuery is not needed. You can do this:
It is that easy! The best way to initiate an HTTP request is with |
||||
|
First write properly. You want to navigate within an application for another link from your application for another link. Here is the code:
And if you want to navigate pages within your application then I also have code, if you want. |
||||
|
You need to put this line on your code
|
|||
|
1.
2.
3.
4.
5.
6.
|
|||
|
Write the below code after the PHP, HTML or jQuery section. If in the middle of the PHP or HTML section, then use the <script> tag.
|
||||
|
In JavaScript and jQuery we can use the following code to redirect the one page to another page:
|
||||
|
Here is a time-delay redirection. You can set the delay time to whatever you want:
|
|||||
|
|
|||
|
Instead of redirecting, you can replace the window the user is on with the page you want to redirect to using JavaScript code like this:
You can also do this:
|
||||
|
Page redirect in jQuery/JavaScript using PHP pagesThis is very simple. See this code:
If you want to give a complete URL as |
||||
|
This is how I use it.
|
|||||||||
|
Check this link // similar behavior as an HTTP redirect
// similar behavior as clicking on a link
|
||||
|
You can use it like in the following code where getGuestHouseRequestToForward is the request mapping (URL). You can also use your URL.
This is for the same context of the application. |
||||
|
Use the following code:
|
|||||||||
|
protected by Community♦ Mar 27 '12 at 17:03
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?
window.location
is the same aswindow.location.href
, in terms of behavior.window.location
returns an object. If.href
is not set,window.location
defaults to change the parameter.href
. Conclude: Use either one is fine. – Raptor Aug 7 '12 at 7:03window.location.replace
and it's advantage over window.location.href, and my 300000+ users will be happier when they hit the back button, so it yes, it deserves a big +1 – Samuel Rossille Jan 28 '13 at 22:36