How do you redirect to a page from another page with JavaScript?
-
1You need a piece of Javascript to redirect to a CI view? – joelcox Jan 20 '11 at 08:12
-
5Does it have anything to do with codeignite or did you just choose random tag? – Shadow The GPT Wizard Jan 20 '11 at 08:44
-
2I suggest you to use this [URL redirect generator — with no-script & SEO support](http://insider.zone/tools/client-side-url-redirect-generator/) It has a build in IE hack to pass the referrer. – Patartics Milán Aug 26 '15 at 13:07
-
location.replace("url"); or window.location.replace("url"); – Jack jdeoel Sep 18 '15 at 02:59
-
– Nazren Naz Apr 30 '19 at 06:35
6 Answers
To redirect to another page, you can use:
window.location = "http://www.yoururl.com";
-
5
-
56@JFA You could embed the window.location in a timeout function, like this: `t1 = window.setTimeout(function(){ window.location = "http://www.yoururl.com"; },3000);` where 3000 is 3 seconds. – TARKUS Oct 27 '15 at 17:22
-
16window.location.href = "example"; is probably better practice because browser policies might restrict its use and block it since .location and .location.href are not exactly the same. However in some cases using .location is ideal particularly if you're using same origin policies like an iframe. – phpvillain Mar 17 '16 at 01:40
-
can you provide some example in what to do in case a if else conditional redirection is required ? – Amitya Narayan Aug 16 '16 at 06:55
-
2
-
This, however, will not show up in the user's history, so you can use: `window.location.href = "http://yoururl.com";` instead. – George Ogden May 29 '20 at 17:56
-
instead of url how to to make it redirect to another jsp page on the same path? – Ragas Nov 05 '20 at 09:29
window.location.replace('http://sidanmor.com');
replace()
does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.
If you want to simulate someone clicking on a link, use
window.location.href
If you want to simulate an HTTP redirect, use
window.location.replace
For example:
// similar behavior as an HTTP redirect
window.location.replace("http://sidanmor.com");
// similar behavior as clicking on a link
window.location.href = "http://sidanmor.com";
Taken from here: How to redirect to another page in jQuery?
-
8This is definitely the correct answer, but it should be noted that `replace()` might not always be the best option. If one is redirecting after an AJAX call completes or something, keeping the originating page in history might be expected. It really depends on the situation! – dlkulp Jan 21 '19 at 20:07
-
11**This answer is totally misleading!** There is no "better", especially no bold-written "better". Using `replace` or `href` depends on the use-case. I personally mostly use `href` because most of the times i need to let the user navigate back. – Mick Jul 16 '19 at 13:01
-
1This answer underlines an important detail. Instead of saying that is it "better", you should just explain the difference, and let the reader chose the most appropriate. Anyway, thanks for your answer, `replace` is the way to go in my situation :P – Boiethios Feb 14 '20 at 12:03
You can't redirect to a function. What you can do is pass some flag on the URL when redirecting, then check that flag in the server side code and if raised, execute the function.
For example:
document.location = "MyPage.php?action=DoThis";
Then in your PHP code check for "action" in the query string and if equal to "DoThis" execute whatever function you need.

- 66,030
- 26
- 140
- 208
-
1@nouvist sorry but I don't agree. Code should only check with simple `if` whether the querystring parameter "action" is equal to "DoThis". It's not using it inside a query, and not evaluating it. If someone choose to do either of those, I can't really control it – Shadow The GPT Wizard May 18 '21 at 11:51
- If you want to simulate someone clicking on a link, use
location.href
. - If you want to simulate an HTTP redirect, use
location.replace
.
For example:
// Similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// Similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
Information copied from this answer to a duplicate question

- 107,154
- 87
- 232
- 265
You may need to explain your question a little more.
When you say "redirect", to most people that suggest changing the location of the HTML page:
window.location = url;
When you say "redirect to function" - it doesn't really make sense. You can call a function or you can redirect to another page.
You can even redirect and have a function called when the new page loads.

- 7,904
- 4
- 42
- 42

- 241,084
- 71
- 387
- 401
Compared to window.location="url";
it is much easyer to do just location="url";
I always use that

- 64,414
- 37
- 100
- 175

- 1
-
7I would avoid this, because under certain circumstances, the variable "location" isn't available, and could be created as global. Plus, if the programmer mistypes it slightly, they create a new global. It's much better to be verbose. – Richard Duerr Oct 14 '16 at 17:20