- On changing the URL structure of your domain.
- To enable redirection based on location or language.
- The user submits a form, and you have to bring them back to the next page in a sequence.
- Put pages which require authorization
- Redirecting users to HTTPS from HTTP
- Trigger a new page with a single page application.
- hash – Usually for the single-page websites and apps
- host – the domain of the URL
- hostname: - similar function as of host
- href – the domain name in full
- origin - URL protocol and the domain
- pathname - URL slug after the origin
- port – if there is a port put in URL
- protocol – various protocols like https, HTTP, FTP, FTPS, and so on.
Each of the above properties can support the standard strings for actions like replace. Some of the object methods for location are:
- assign – to set location object to a new URL
- reload – instructs to reload the page with the same URL.
- replace - triggers redirect
- search – to interrogate queryString
You may find the functions of Replace and Assign similar, but each has varying constraints. Assign can load resources at the redirected URL and can preserve previous entry in the navigation history of the browser. So, the user can get back to the previous page by hitting the back button. Replace method also does the same thing, but the original page of the ‘current' resource will not be there in the browsing history. So, the user cannot go back to the previous page by hitting the back button. However, both of these functions may fail if the restrictions to ensure security as if you cannot use third party scripts to trigger an action. It is actually good as many sites tend to use third-party scripts even without code verification.
Redirecting to a new address
It is also recommended to configure the server also to send appropriate HTTP redirection headers which are so efficient. It is applicable to both redirecting to new addresses as well as from HTTP to HTTPS.
If it's readily available, the default setting can reload a page from the cache of the browser. You may also force the bypass method to the local cache and do document retrieval from the network. The below snippet of code is useful for this.