Coder Perfect

How do I use JavaScript to redirect? [duplicate]

Problem

With JavaScript, how do you redirect to a page from another one?

Asked by Lucky13

Solution #1

You can use the following code to go to another page:

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

Answered by seedg

Solution #2

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

It’s preferable to window.location. href=”http://sidanmor.com”>href=”http://sidanmor.com”>href=”http://sidanmor.com

Replace() is preferable since it removes the initial page from the session history, preventing the user from becoming trapped in a never-ending back-button saga.

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 this website: How to use jQuery to redirect to another page.

Answered by sidanmor

Solution #3

A function can’t be redirected to. When redirecting, you can pass a flag on the URL, check that flag in the server side code, and if it’s raised, call the function.

For example:

document.location = "MyPage.php?action=DoThis";

Then, in your PHP code, look for the word “action” in the query string and, if it’s equal to “DoThis,” call whatever function you want.

Answered by Shadow Wizard Hates Omicron

Solution #4

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";

This information was copied from a duplicate question’s response.

Answered by 5 revs, 2 users 86%

Solution #5

It’s possible that you’ll need to expand on your question.

When you say “redirect,” most people think of changing the HTML page’s location:

window.location = url;

It’s not clear what you mean when you say “redirect to function.” You have the option of calling a function or redirecting to another website. You may even redirect to a new page and have a function run when it loads.

Answered by Fenton

Post is based on https://stackoverflow.com/questions/4744751/how-do-i-redirect-with-javascript