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">
window.location.href="http://www.example.com/";
</script>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">
location.replace('http://www.example.com/');
</script>
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))
{
window.location.replace('example.html');
}
</script>23.02.2007. 10:45
Roger Blood said on 20.09.2007. 22:57
Hey this worked great, Thanks Man! i was able to redirect my blog to my website!
Mike said on 17.11.2007. 14:28
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?
Eric So said on 10.12.2007. 23:07
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.
kalman said on 13.12.2007. 17:39
thanks, simple and works great
the "location.replace" method works best though.
mgwalk said on 07.03.2008. 02:51
I never knew of location.replace
thanks
Graham said on 01.04.2008. 16:13
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.
e.g.
english/level1/level2/index.htm
to
welsh/level1/level2/index.htm
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
Andy said on 08.04.2008. 13:39
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
Hello
I own a domainame, but you can also acces the site via users.ispname.com/login. Is redireecting with javascript from users.ispname.com/login/actual_file_name.html to mydomain.com/the_entered_file_name.html ?
Thanks
John said on 07.05.2008. 04:19
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.
abhishek said on 01.07.2008. 09:02
hi
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.
javascript Lover said on 16.07.2008. 08:29
short but informative post. Nice one.
SNVC said on 23.08.2008. 14:00
Thanks for this tutorial. I had been wondering how to do that until i read this.
Recent Comments:
Page last (manually) updated: May 01, 2007.
Questions, comments, insults or praise? Have your say: