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

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

12.7k1982110

asked Jan 20 '11 at 8:08


RAWhoLovesMusic

4,47031428

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.


1

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

5

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

2

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

1324

down vote

accepted

To redirect to another page, you can use:


window.location = "http://www.yoururl.com";

shareimprove this answer

edited Feb 17 '15 at 15:55


Dorian

11.6k36878

answered Jan 20 '11 at 9:52


seedg

16k73255

4

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

39

@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

11

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

1

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

add a comment

up vote

181

down vote

window.location.replace('http://sidanmor.com');

It's better than using window.location.href = 'http://sidanmor.com';


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

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?


shareimprove this answer

edited Nov 27 '17 at 19:57


E. Villiger

651318

answered Oct 25 '16 at 7:14


sidanmor

2,61821424

3

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

add a comment

up vote

48

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

55.6k17104163

add a comment

up vote

28

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


user75525

140k40268298

add a comment

up vote

23

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

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


// Similar behavior as clicking on a link

window.location.href = "http://stackoverflow.com";

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