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>
Category: 

Comments

Thank you for sharing a Gold.

Thank you for sharing a Gold.=)

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.

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 = "http://www.name.com";
}
else
{
history.back();
}

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!

http://online-marketing-technologies.com/tools/javascript-redirection-ge...

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

I am looking for a script

I am looking for a script that does:

redirect to page "mypage.xyz.com"
if the page is blocked by my office proxy (or websense, etc)
redirect to page "mypage.abc.com"

any ideas?

Hi Andy ,

Hi Andy ,

I am redirecting .page to .pdf and after redirection I want to disable back button . Please help me in this . Thanks in advance .

Regards,
Satyam

dear sir,

dear sir,
i d like to inform me if at the last rediraction with the critiria:

<script language="JavaScript" type="text/javascript">
if ((screen.width>=1024) && (screen.height>=768))
{
window.location.replace('example.html');
}

</script>

we have the ability to put something like that:

if user copy the url of the browsers adress bar

.....
i mean i want a javascript that can redirect the user just if he try to copy the url of my page's url shown into the browsers adress bar.
thank you

does anyone know how to make

does anyone know how to make this work for a tumblr blog? when i tried it, my blog refused to load.

Add new comment