Create a redirect page with Javascript

danejCode Snippets

When you have a complicated marketing funnel, sometimes you need to “fire a pixel” on certain pages to ensure your tracking picks up the progress of your visitor. This is typically a piece of javascript that needs to run or it may be an image that loads on a 3rd party server.

To redirect the page using javascript:

window.location.replace("http://your.domain.com");

You can also use a meta refresh for older browsers or someone that has disabled javascript.

<meta http-equiv="refresh" content="5; URL=http://your.domain.com">

The “5” in this case refers to how many seconds to wait before redirect.

What if you wanted to wait 3 seconds before redirecting? In javascript, you can use the setTimeout function.

  setTimeout(function() {
    window.location.replace("http://your.domain.com");
  }, 2 * 1000);  // wait 2 seconds