Page Redirect in ES6

The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page.

“The page redirection is also referred to as a way to redirect one web page to another web page.” The redirection can be performed on the same or different server. We cannot redirect a page to a different page that is not requested. The page redirection can be performed with the help of various methods. For example, location.replace(), location.href etc.               

Now, we are going to elaborate some page redirection methods used in JavaScript. They are given below:

Window.location.href and Window.location

Window.location Object refers as a window object property. We can redirect a web page with the help of various methods. Generally, page redirection method is related to the window.location object.

This object can be used to get the address current web page or the URL. We can write the window.location without using the prefix window.

Example

We have an illustration of the same.

location.assign()

 location.assign method is used to provide a new document to the browser.

Syntax

location.replace()

It is widely used as window.location object. We can replace the existing document with a new one with the help of location.replace() method.

Syntax

Example                                                                                                    

Here, we have an example to understand the above method.                         

Difference between location.replace() and location.assign

The essential difference between location.replace() and location.assign() is-

location.replace() method deletes the current URL from history. So, we cannot go back to original document.

location.assign() method loads a new document in web browser. So, it is possible to go back to the original document.

location.reload()

It is used to reload the existing document on the web browser dynamically. This method also returns the Boolean parameter (true or false). If it returns true, then the method reloads the page from the server. Otherwise, it reloads the page from history and returns false.

Example

Window.navigate()

The window.navigate() method is similar to window.location.href(). It only supports the Internet explorer browser.

Example

window.nevigate(“https://www.Tutorialandexample.com/”);

Page redirecting and Search Engine Optimization (SEO)

The search engines do not evaluate the JavaScript to check the page redirection. If we want to alert the SEO about URL redirecting, we must add the rel = “canonical” tag in the web browser.

Example

Pin It on Pinterest

Share This