Wednesday, January 16, 2013

HOWTO Redirect A Page Using Javascript

Occasionally you need to move a page on a website from one location to another, but don't have the ability to update all of the links that point to the old one or access to the server software to do the same thing.

All you need is a page with a little Javascript to solve the problem.

Example

<html>
<head>
<title>Redirecting...</title>
<script type='text/javascript'>
window.location.replace("http://your.site.here");
</script>
</head>
<body>
<p>Redirecting...if you are not redirected click
<a href='http://your.site.here'>here</a>.</p>
</body>
</html>

Notes

  • Include a link and Javascript to redirect for users that have JS turned off.
  • Use window.location.replace instead of window.location.href so the user doesn't get stuck in a loop when they press the back button.
  • Use window.location.replace instead of document.location
  • Insert if(top != self) top.location.replace("http://your.site.here"); in the top of the <script> tag to escape from frames.

No comments:

Post a Comment