Skip to content
Related Articles

Related Articles

Improve Article

ES6 | Page Redirect

  • Last Updated : 11 Oct, 2019

The ES6 page redirect is used to send a request to different website addresses to the user and browser search engine, (search engine and user received different website address to that was requested by the search engine or the user). Redirecting to a different page that was not requested by the user or by search engine can be on the same server or maybe on a different server. Also, it can be a different website.
To redirect into another page that was not requested is done by using the JavaScript latest version of ES6. There are lots of methods that can be used to redirect to another page, all the methods are listed below with the description. Remember one thing that all the methods belong to a single-window return object.

  • location.replace() Method: This method will replace the current website location to the redirected website location by using the .replace() method.
    Syntax:

    Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

    window.location.replace = "Your redirected link"
  • location.assign() Method: This method will assign a new location to the redirected website location by using the .assign() method.
    Syntax:
    window.location.assign = "Your redirected link"
  • location.reload() Method: This method will reload the current document by using the .reload() method.
    Syntax:



    window.location.reload = "Your redirected link"
  • window.navigate() Method: This method can be used in Internet Explorer only all the other browsers remove this method. So it is good to be avoided because other browsers will not support this method. This method is similar to the .location.assign() method. This method assigns a new value that will be navigated by using the .navigate() method.
    Syntax:
    window.navigate  = "Your redirected link"

Below example will illustrates the whole concept of Page redirecting:
Example:




<!DOCTYPE html> 
<html
<head
    <title>
        ES6 | Page Redirect
    </title>
      
    <script
        function geeks() { 
            window.location = 
                "https://ide.geeksforgeeks.org/tryit.php"; 
        
    </script
</head
  
<body style="text-align:center;">
       
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
      
    <input type = "button" 
        value = "Redirecting Page"
        onclick = "geeks()"
</body
</html>   

Output:

  • Before clicking the button:
  • After clicking the button:

Note: Add the rel = “canonical” inside the head section of the web page to inform the search engine when you are using the page redirecting method.

<link rel = "canonical" href = "Redirecting Page" />



My Personal Notes arrow_drop_up
Recommended Articles
Page :