How to redirect user to another page in JavaScript / jQuery?

I would like to know what redirect methods exist in Javascript, and also using jQuery, and if possible, I would like to know all of them.

Author: Paulo Roberto Rosa, 2014-01-30

4 answers

In this case jQuery is not accurate.
The solution goes through simple javascript.
here are two options:

Option 1:

window.location.href = "http://pt.stackoverflow.com";
// ou uma variante com o mesmo efeito
window.location.assign("http://pt.stackoverflow.com");

Option 2:

window.location.replace("http://pt.stackoverflow.com");

The difference between these two methods is that the first makes it possible to click back in the browser history and go to the previous page. The second I replaced the current page and when clicking to go back in the story, the home page is not accessible (it was replaced in the history).


If you want the user to open A New Window (without losing the source window), you can use:

window.open("http://pt.stackoverflow.com");

This method window.open() it also allows you to pass parameters / options such as size, content, etc.

 59
Author: Sergio, 2014-01-30 21:51:21

Via jQuery (only for knowledge if it is your desired)

$(location).attr('href', 'http://www.sitedesejado.com');

However, as already mentioned, jQuery is not required to perform redirection.

 9
Author: Daniel, 2014-01-30 21:41:09

With javascript same:

window.location.href = "http://seusite.com"
 8
Author: Maurilyn, 2014-01-30 21:21:08

As mentioned above just use the command:

window.location="http://seusite.com";

No need for href, minimizing your code.

 3
Author: Adilmar Coelho Dantas, 2014-01-31 00:02:46