return
return

Reputation: 1049

How to redirect from one URL to another URL?

How can I redirect to another URL in a web page using JavaScript?

Upvotes: 34

Views: 133207

Answers (7)

Maniruzzaman Akash
Maniruzzaman Akash

Reputation: 5025

You can redirect anything or more URL via javascript, Just simple window.location.href with if else

Use this code,

<script>
if(window.location.href == 'old_url')
{
    window.location.href="new_url";
}


//Another url redirect
if(window.location.href == 'old_url2')
{
    window.location.href="new_url2";
}
</script>

You can redirect many URL's by this procedure. Thanks.

Upvotes: 11

jigish desai
jigish desai

Reputation: 81

location.href = "Pagename.html";

Upvotes: 1

Jeff
Jeff

Reputation: 4413

Why javascript?

http://www.instant-web-site-tools.com/html-redirect.html

<html>
<meta http-equiv="REFRESH" content="0;url=http://www.URL2.com"> 
</html>

Unless I'm missunderstanding...

Upvotes: 3

David
David

Reputation: 218798

Since you tagged the question with javascript and html...

For a purely HTML solution, you can use a meta tag in the header to "refresh" the page, specifying a different URL:

<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourdomain.com/somepage.html">

If you can/want to use JavaScript, you can set the location.href of the window:

<script type="text/javascript">
    window.location.href = "http://www.yourdomain.com/somepage.html";
</script>

Upvotes: 26

locrizak
locrizak

Reputation: 12281

you can also use a meta tag to redirect to another url.

<meta http-equiv="refresh" content="2;url=http://webdesign.about.com/">

http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm

Upvotes: -1

user596075
user596075

Reputation:

If you want to redirect, just use window.location. Like so:

window.location = "http://www.redirectedsite.com"

Upvotes: 4

Paul
Paul

Reputation: 36319

window.location.href = "URL2"

inside a JS block on the page or in an included file; that's assuming you really want to do it on the client. Usually, the server sends the redirect via a 300-series response.

Upvotes: 46

Related Questions