Javascript Redirect Scripts

Redirecting a visitor with javascript is pretty straightforward. The simplest way is to use one of the methods below.

Note that in some cases a server-side redirect (i.e. one using a language such as PHP, ASP or Perl) is a better choice, since not all users will have javascript enabled. Search engine spiders are unlikely to follow javascript based redirects.

Location.href Method

<script type="text/javascript">

Including this script on a page will immediately redirect visitors to the URL entered.

Location.replace Method

The difference between location.href and location.replace is that the former will create a new history entry on the visitors computer. This means that if they hit the back button, they can get stuck in a 'redirection loop'. This is usually undesirable and may have unwanted side effects - most pay per click search engines will not allow the submission of URLs that 'break' the back button.

The solution is to use location.replace instead:

<script type="text/javascript">

Conditional Redirects with Javascript

Once you know how to redirect visitors, you can send them to different pages based on a variety of criteria. The example script below will redirect visitors with a resolution of 1024x768 or higher to a different page. Of course, there shouldn't be any reason to do so for most websites, which should work at any screen resolution ;)

<script language="JavaScript" type="text/javascript">
if ((screen.width>=1024) && (screen.height>=768))



It was useful. I got what I

It was useful. I got what I was searching for.

I'm just a novice in

I'm just a novice in javascript. This was a great tip! Thanks!

Hey this worked great, Thanks

Hey this worked great, Thanks Man! i was able to redirect my blog to my website!

great tip!

great tip!

I want to redirect visitors

I want to redirect visitors to a different URL after they have closed the page. EG they click the X(top left of the browser) and this triggers a redirect to a new site: is this possible?

I need to redirect users to

I need to redirect users to one of two possible URLs based on what they select on a form. For example, if a person selects a field "A" on a form, when the click submit, they will be taken to URL A.

thanks, simple and works

thanks, simple and works great

the "location.replace" method works best though.

I never knew of location

I never knew of location.replace

I want to run two parallel

I want to run two parallel sites (two languages) and put a link on each page so that I can swap languages by swapping to a parallel set of directories.


Basically anywhere in one side to its mirror on the other.

I would like to be able to dismantle the url of the current page then reassemble it replacing 'english' with 'welsh' or vice versa.

Is this possible in javascript?

gjl at aber dot ac dot uk

Hi Graham,

Hi Graham,

This is easy to do: the current URL is stored within location.href. So, all you need to do is check whether 'welsh' or 'english' is within this string:

if (location.href.match('welsh')) {
// the page is in the welsh directory

else {
// the page is in the english directory


If you can do server-side stuff then you could do this behind the scenes by matching the language setting of the visitor's browser and redirecting or displaying content appropriately, which would be the preferred approach for this sort of thing


I own a domainame, but you can also acces the site via Is redireecting with javascript from to ?


Great redirect. Freeservers

Great redirect. Freeservers sucks when it comes to repointing a domain to another web hosting company. Using this redirect at least gets my domain up and running for now until customer unfriendly Freeservers gets their act together.
Many thanks.

Very useful tips. Thank for

Very useful tips. Thank for the information.


i want to redirect a page to login page when user is not logged in suppose i want to access another page of site without login then after entering in url... login page nust be open...
plese help me.

short but informative post.

short but informative post. Nice one.

Thank you for sharing a Gold.

Thank you for sharing a Gold.=)

Thanks for this tutorial. I

Thanks for this tutorial. I had been wondering how to do that until i read this.

very usefull tricks thanks

very usefull tricks

Hi Andy !

Hi Andy !
The script snippet is great.
I slightly modified it for my purpose. I recently changed blog's publishing address from to
Now, the earlier blog had lots of raffic from Google search. But when I moved, I wanted the traffic to be redirected to the page in my new blog.

I modified the snippet as:

var newLoc = strLoc.replace("lonelyreddevil", "sdqali");

hope this helps somebody..

This really helped with my

This really helped with my University Coursework, I referenced this page in my code, thanks very much!

Why does the redirect happen

Why does the redirect happen as POST request? Is there any way to make it use GET instead?

Hi bluedome,

Hi bluedome,

As far as I'm aware this type of javascript redirect will always result in a GET request. It sounds like something specific to your implementation. how are you checking the headers?

thank for share knowladge.

thank for share knowladge. krab .(thailand)

Hello all,

Hello all,

Excellent post. I'm using the location.replace method to redirect from my geocities url to my url. Is there a way to have the redirect also change the url in the browser's address field, as this is ideally what I'm trying to do? Right now, the script redirects, but the address bar still shows geocities address.


Sounds like a browser bug,

Sounds like a browser bug, William - location replace physically changes the URL, so the new one should always show in the address bar if the redirect had taken place. Do you get the same effect in all the browsers you've tried?

This is a great post and gets

This is a great post and gets me close but what I need is help with script that redirects home(index) to enter page unless cookie has been set by onclick cookie set from previous visit to enter page.
I can find plenty of scripts to redirect if a cookie is present but not much on redirect if no cookie present.
Please help, I'm a javanovice and in desperate need of guidance.

Thanks a was really

Thanks a was really helpfull!



Very informative and extremely useful.

Is there a way of introducing a conditional element, so that if URL A fails to load within x seconds, the browser can automatically revert to an alternate URL B?

Thanks this was a very quick

Thanks this was a very quick help, exactly what I was searching for.

hey, thanks for the js

hey, thanks for the js redirect functions!

Your Conditional Redirects

Your Conditional Redirects with javascript is very neat! Appreciate the info!

Why would a group e-mail from

Why would a group e-mail from the Treasurer that asks each member to double click the typed president's name in the e-mail direct them to a Yahoo web page instead of bringing up an e-mail form with the president's name as the To: address.

Hi, I have come across

Hi, I have come across strange thing like setTimeout('window.location=../',1000). Where the page is redirected if "window.location=../" is used? Need help

This solved my redirect

This solved my redirect problem. I also found all the information I needed about window.location object here.

good info. replace is the

good info. replace is the ticket for me

Thanks for useful snipets.

Thanks for useful snipets.

I can see since 2007, many

I can see since 2007, many people like me found this page useful. I hope you keep the good work. Thanks a heap!

Many thanks this is great

Many thanks this is great,redirect blogger to glogger.

Very Interesting Information!

Very Interesting Information! Thank You For This Blog!

You can also use this:

You can also use this:

if(confirm("This page has been moved to a new location... would you like to be redirected?"))
location = "";

Does all the javascript

Does all the javascript redirect script compatible about SEO?

Thanks very much

Thanks very much

I want to redirect visitor to

I want to redirect visitor to different login page for each visitor using javascript, Can anybody help me out...

great post! thanks for

great post! thanks for sharing

It is a very awesome

It is a very awesome information.

Cool scripts thanks

Cool scripts thanks

I'm having some problems

I'm having some problems getting any version of IE to redirect to a new URL.

At the minute I am reading an xml response and between 'content' tags is my URL I want to redirect to (it is different everytime).
I'm using this method to redirect users currently:

window.location.replace = content;
(content being my URL from xml response)

Works fine in everything but IE! Can anyone offer a different method that I can try? Any help would be much appreciated!

Strange - with all of the

Strange - with all of the positive comments here, I assume this really works, but try as I might,

window.location=sTargetURL works
window.location.href=sTargetURL works

window.location.replace=sTargetURL does nothing

(Using javascript)

Andy Langton's picture

Did you forget your brackets?

Did you forget your brackets?


Nearly i wasted 2 hours for

Nearly i wasted 2 hours for this. Its awesome. Thank you!

i want to redirect a user to

i want to redirect a user to another url/page when the close button is click [X], please is it possible?

Tnx I want something

Tnx I want something different,
I want make my visitors or users submit posts themwselves and after uploading the data(text and images) it show up on homepage as real posts.

Is it possible?

pls help me,
it would be appreciated

By creating a redirect you

By creating a redirect you can build some amazing landing pages to do most of the work when driving traffic.Thanks much for the code...

Excellent post. Thanks for

Excellent post. Thanks for sharing.

Hello this is surya prakash

Hello this is surya prakash from india. I want to know, how the page location will not change while and after reloading the page in php coading...

i love u dude...........

i love u dude........... thanks for free help............................

Gracias, me ahorraste un poco

Gracias, me ahorraste un poco de tiempo.

thanx... actually m opening a

thanx... actually m opening a webpage in a fixed size container, can u plz tell me how resize the webpage that will get opened in that container. In short, I want to avoid the scroll bars, and the page size should be exactly equal to the size of the container..plz help

Hi ,

Hi ,

I want to redirect users to specific links, what i mean is if original link is ABC then system should redirect it for 'Z' and if link is BAC it should redirect to 'A'..based on the orignal link system should appropriately redirect it on the fly, Can anyone suggest me a solution

I think location.replace(url)

I think location.replace(url) is a much better way to redirect users automatically. If you redirect on page load, try to use the rel="canonical" to pass the link juice too!

This javascript redirect script generator will generate you fully compatible and much more sophisticated redirection scripts.

Add new comment