Skip to content

Instantly share code, notes, and snippets.

@lxneng
Created June 10, 2011 02:40
Show Gist options
  • Save lxneng/1018152 to your computer and use it in GitHub Desktop.
Save lxneng/1018152 to your computer and use it in GitHub Desktop.
How can I make a redirect page in jQuery?

jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.

It is better than using window.location.href =, because replace() does not put the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco. If you want to simulate someone clicking on a link, use location.href. If you want to simulate an HTTP redirect, use location.replace.

For example:

// simulates similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// simulates similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

jQuery way

For example

var url = "http://stackoverflow.com";
$(location).attr('href',url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment