JavaScript

Javascript Redirect URL example

It has happened to all of us to click a certain URL and be redirected to another one. Probably you even wondered why did that happen.

Well, possibly the page moved to a new, more suitable domain or the programmer has built versions of the same page that depend of the type of browser you use so you are redirected accordingly. How can you do the same with the website you’re building? Look below!
 
 
 
 
 
 


 

Straightforward redirection

Straightforward redirection is done by changing the window.location property. By doing that, you will have replaced the current webpage with the one specified by the URL. You would only have to add the following code snippet to your webpage like below:

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

Here we have set the page URL to http://www.webcodegeeks.com/, which is the page that will load when the user clicks the previous URL.

Timed delay redirection

Let’s suppose you want to tell something to your user before doing the redirection. That would need the use of the timed delay to allow for time util the user reads your message. Take a look at the code below:

function Redirect() {
	window.location="http://www.tutorialspoint.com";
}

document.write("You will be redirected to our new website in 10 sec.");
setTimeout('Redirect()', 10000);

First we have turned the classic redirection into a function named Redirect(), then written our message and then we have called the function Redirect(), using a Javascript built-in feature such as setTimeOut(), giving the latter also the time delay in milliseconds.

Browser based redirection

One of the most used cases of redirection is when you want to show users different webpages based on their browsers. See the code below:

var browser = navigator.appName;
if( browser == "Google" )
{
   window.location = "http://www.mywebsite.com/google.html";
}
else if ( browser == "Mozilla Firefox" )
{
   window.location="http://www.mywebsite.com/firefox.html";
}
else
{
   window.location="http://www.mywebsite.com/other.html";
}

First we get the browser’s name, and then if it is Google we redirect to the google.html view of our website. If it is Mozilla Firefox, we do the same redirecting to the firefox.html view of it, and if the browser used is another one except from these two we redirect it to a special view for all of them, obviously, except Google and Firefox.

This is how we redirect URL’s using Javascript.

Download the source code

This was an example of URL redirection in Javascript.

Download the source code for this tutorial:

Download
You can download the full source code of this example here : RedirectURL

Era Balliu

Era is a Telecommunications Engineering student, with a great passion for new technologies. Up until now she has been coding with HTML/CSS, Bootstrap and other front-end coding languages and frameworks, and her recent love is Angular JS.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Fred
Fred
9 years ago

Could also do:

setTimeout(‘window.location=”http://www.tutorialspoint.com”;’, 5000);
or
setTimeout(function() { window.location=”http://www.tutorialspoint.com”; }, 5000);

Cincin Kawin
8 years ago

First of all I would like to say terrific blog! I had
a quick question that I’d like to ask if you do not mind.
I was curious to find out how you center yourself and clear your head
prior to writing. I’ve had a difficult time clearing my
thoughts in getting my ideas out. I do enjoy writing however it just seems like the
first 10 to 15 minutes are generally lost simply just trying to figure out how to
begin. Any ideas or hints? Thanks!

Back to top button