How can I redirect the user from one page to another using JavaScript or jQuery?
Join them; it only takes a minute:
One does not simply redirect using jQueryjQuery is not necessary, and
For example:
|
|||||||||||||||||||||
|
|
|||||||||||||||||||||
|
If you are here because you are losing HTTP_REFERER when redirecting, keep reading: The following section is for those using
Testing against Simple cross-browser testing solution (fallback to window.location.href for Internet Explorer 9+ and all other browsers) Usage:
|
|||||||||||||||||
|
Use:
|
|||||||||||||||||||||
|
This works for every browser:
|
|||||
|
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.
|
|||
|
I also think that 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 has Internet Explorer support to pass the HTTP referrer. Sample code without delay looks like this:
|
||||
|
But if someone 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. |
|||||||||||||
|
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. |
||||
|
Should just be able to set using Example:
Here is a past post on the subject: |
|||||||||
|
Before I start, jQuery is a JavaScript library used for DOM manipulation. So you should not be using jQuery for a page redirect. A quote from Jquery.com:
It was found here: https://jquery.com/browser-support/ So jQuery is not an end-all and be-all solution for backwards compatibility.
This page will redirect to Google after 3000 milliseconds
Different options are as follows:
When using replace, the back button will not go back to the redirect page, as if it was never in the history. If you want the user to be able to go back to the redirect page then use You can also use meta data to run a page redirect as followed. META Refresh
META Location
BASE Hijacking
Many more methods to redirect your unsuspecting client to a page they may not wish to go can be found on this page (not one of them is reliant on jQuery):
I would also like to point out, people don't like to be randomly redirected. Only redirect people when absolutely needed. If you start redirecting people randomly they will never go to your site again. The next part is hypothetical:
Please review Google Webmaster Guidelines about redirects: https://support.google.com/webmasters/answer/2721217?hl=en&ref_topic=6001971 Here is a fun little page that kicks you out of the page.
If you combine the two page examples together you would have an infant loop of rerouting that will guarantee that your user will never want to use your site ever again. |
|||||||||||||||||||||
|
|
||||
|
You can do that without jQuery as:
And if you want only jQuery then you can do it like:
|
|||||
|
This works with jQuery:
|
||||
|
# HTML Page Redirect Using jQuery/JavaScript Try this example code:
If you want to give a complete URL as |
||||
|
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 stackoverflow.com. And that's how you make a Simple redirect page with JavaScript Edit: There is also one thing to note. I have added Example:
So, if this suits your needs then everything should be fine. If you want to include the redirect page in the browser history replace this
with
|
||||
|
On your click function, just add:
|
|||||
|
|
||||
|
jQuery is not needed. You can do this:
It is that easy! The best way to initiate an HTTP request is with |
|||||||||||||
|
You can redirect in jQuery like this:
|
||||
|
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. |
|||||
|
To just redirect to a page:
Or if you need a delay:
jQuery allows you to select elements from a web page with ease. You can find anything you want in a page and then use jQuery to add special effects, react to user actions, or show and hide content inside or outside the element you have selected. All these tasks start with knowing how to select an element.
Imagine someone wrote a script/plugin that is 10000 lines of code?! Well, with jQuery you can connect to this code with just a line or two. |
||||
|
In JavaScript and jQuery we can use the following code to redirect the one page to another page:
|
||||
|
You need to put this line on your code
|
|||||
|
There are three main ways to do this,
and...
The last one is best, for a traditional redirect, because it will not save the page you went to before being redirected in your search history. However, if you just want to open a tab with JavaScript, you can use any of the above.1 EDIT: The |
|||||
|
I just had to update this ridiculousness with yet another newer jQuery method:
|
||||
|
Here is a time-delay redirection. You can set the delay time to whatever you want:
|
|||||
|
In JavaScript and jQuery we use this following code to redirect the page:
But you can make a function in jQuery to redirect the page:
And call this function:
|
|||||||||||||
|
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.
|
||||
|
ECMAScript 6 + jQuery, 85 bytes
Please don't kill me, this is a joke. It's a joke. This is a joke. This did "provide an answer to the question", in the sense that it asked for a solution "using jQuery" which in this case entails forcing it into the equation somehow. Ferrybig apparently needs the joke explained (still joking, I'm sure there are limited options on the review form), so without further ado: Other answers are using jQuery's This answer also abuses it, but in a more ridiculous way. Instead of using it to set the location, this uses The function is named The "85 bytes" is a reference to Code Golf. Golfing is obviously not something you should do outside of code golf, and furthermore this answer is clearly not actually golfed. Basically, cringe. |
||||
|
protected by hjpotter92 Nov 12 '15 at 13:11
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
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:03