How to redirect a web browser

Here are three example scripts, to show how a browser can be redirected to a different page with JavaScript. They are useful if you want to rename or move a page that has links to it from other locations. You can put the redirect script in the old page, to send visitors to the new one.

Script #1 – Stealth Redirect

This allows you to redirect a page without telling your visitor, they probably won’t even notice it happen.

<html>
<script>
    location = "http://i-code.co.uk/index.php";
</script>
</html>

Script #2 – Alert Redirect

If you want visitors to know that the page has been moved you could use this script. It uses a JavaScript alert, to tell your visitor that the page has been moved, then when they click OK, their browser is redirected.

<html>
<script>
    alert("This page has been moved to a new location... click OK to be redirected?");
    location = "http://i-code.co.uk/index.php";
</script>
</html>

Script #3 – Confirm Redirect

If you want to give your visitors more control, you can use a confirm dialog. This script gives visitors the option to either be redirected to the new location or go back to the previous page.

<html>
<script>
    if(confirm("This page has been moved to a new location... would you like to be redirected?"))
    {
        location = "http://i-code.co.uk/index.php";
    }
    else
    {
        history.back();
    }
</script>
</html>

Download Scripts