URL or Uniform resource locator, often known as a web address which is a reference to a particular location on a computer network. Technically, when a HTTP request for one page goes to another page automatically, then is it called redirection.
In simple terms, URL redirecting is a way to redirect a web page to any particular location or a page. For example, say you open a website and you see a web page with a link.
All you need to do is assign a URL to it. For example if you want to redirect your web page to “demoxyz.com” then simply write the following code.
Sometimes, you might see window.location instead of window.location.href in some code. Both ways are the same except the difference is that, window.location returns an object. You can observe the difference in your web browsers console.
- URL redirection using replace: The other common method used for URL redirection is the replace method of the window.location object. In the replace method, the current URL is replaced with the previous one in the history.
This means that the back button of the browser can’t be used. The replace method is useful when you don’t want the user to navigate back to the previous URL using the back button. The syntax for this method is:
window.location.replace(“https://mytrashcode.com/”);Also check5 Best IDE for React and ReactJs text editors
- Performing URL redirection using assign(): Another method which you can use for URL redirection is by using window.location.assign() function. This method adds a new URL to the history.
In the replace method you can’t go back to the previous page using the back button, but in the assign method the users will be able to go back to the original page using the back button. Following is the syntax for assign method:
window.location.assign(“ your URL name”);
- Redirecting to an URL using the navigate method: This method is another way or URL redirection. In this method, if available in the cache the page will be loaded from there rather than sending a new request to the server.
However, this method will not if the previous page does not exist in the list. The syntax for this method is:
window.history.back();Also check11 Best Editor for Web Development IDEs trending