MyTetra Share
Делитесь знаниями!
How do I redirect with JavaScript?
Время создания: 19.07.2018 09:56
Текстовые метки: javascript redirect
Раздел: Javascript
Запись: Velonski/mytetra-database/master/base/1531976207csyki9e9s6/text.html на

How do you redirect to a page from another page with JavaScript?

javascript redirect

shareimprove this question

edited Feb 6 at 12:29

Peter Mortensen


asked Jan 20 '11 at 8:08



marked as duplicate by kdgregory, Michał Perłakowski javascript Dec 8 '16 at 19:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


You need a piece of Javascript to redirect to a CI view? – joelcox Jan 20 '11 at 8:12


Does it have anything to do with codeignite or did you just choose random tag? – Shadow Wizard Jan 20 '11 at 8:44


I suggest you to use this URL redirect generator — with no-script & SEO support 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"); – David Jaw Hpan Sep 18 '15 at 2:59

add a comment

6 Answers

active oldest votes

up vote


down vote


To redirect to another page, you can use:

window.location = "";

shareimprove this answer

edited Feb 17 '15 at 15:55



answered Jan 20 '11 at 9:52




How do you make this happen after a few seconds delay? – JFA Oct 17 '15 at 13:26


@JFA You could embed the window.location in a timeout function, like this: t1 = window.setTimeout(function(){ window.location = ""; },3000); where 3000 is 3 seconds. – TARKUS Oct 27 '15 at 17:22


window.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 1:40

can you provide some example in what to do in case a if else conditional redirection is required ? – baymax Aug 16 '16 at 6:55


window.location.href = "";; – Bineesh Apr 12 '17 at 12:22

add a comment

up vote


down vote


It's better than using window.location.href = '';

Using replace() is better because it 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


// similar behavior as clicking on a link

window.location.href = "";

Taken from here: How to redirect to another page in jQuery?

shareimprove this answer

edited Nov 27 '17 at 19:57

E. Villiger


answered Oct 25 '16 at 7:14




Excellent description – Himalaya Garg Jul 2 '17 at 6:16

add a comment

up vote


down vote

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.

shareimprove this answer

answered Jan 20 '11 at 8:46

Shadow Wizard


add a comment

up vote


down vote

You may need to explain your question a little more.

When you say "redirect", to most people that suggests 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.

shareimprove this answer

edited May 4 '14 at 18:46

answered Jan 20 '11 at 8:12



add a comment

up vote


down vote

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


// Similar behavior as clicking on a link

window.location.href = "";

Так же в этом разделе:
MyTetra Share v.0.60
Яндекс индекс цитирования