July 30, 2014

page redirect in Javascript


JavaScript Location Object also have these three methods
  1. assign() -- Loads a new document.
  2. reload() -- Reloads the current document.
  3. replace() -- Replaces the current document with a new one
You can use assign() and replace methods also to redirect to other pages like these
window.location.assign("http://www.stackoverflow.com");

window.location.replace("http://www.stackoverflow.com");
The difference between replace() and assign()  is that replace() removes the URL of the current document from session history, means it is not possible to use the "back" button to navigate back to the original document. So use the assign() method if you want to load a new document, and also want to navigate back to the original document.
Apart from the above,
window.location.href = "http://stackoverflow.com";
This is basically simulating the click of a link

Reference

No comments:

Post a Comment