Open In App

How to Get Browser to Navigate URL in JavaScript ?

Last Updated : 06 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

As a web developer, you may need to navigate to a specific URL from your JavaScript code. This can be done by accessing the browser’s window object and using one of the available methods for changing the current URL.

In JavaScript, there are several approaches for navigating to a URL. The most commonly used methods are:

Using window.location.href: This method is used to get or set the entire URL of the current page. It returns a string containing the current URL, and can also be used to navigate to a new URL by setting it to the desired value.

Approach: Using window.location.href: This approach sets the entire URL of the current window to the specified URL. This method loads the new URL in the same window and adds an entry to the browser’s history. The window.location property in JavaScript is used to get or set the current URL of the browser window. It is a read-only property that contains information about the current URL including the protocol, domain name, port number, path, and query string. In this example, the href attribute is set to “#” to prevent the browser from navigating to a different page when the link is clicked. Instead, the onclick attribute is used to execute a JavaScript function that sets the window.location.href property to the desired URL.

Syntax:

window.location.href = "https://www.gfg.com";

Example: This HTML code links with the GeeksforGeeks home page when the button is used.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        Linking to GeeksforGeeks 
        using JavaScript
    </title>
</head>
  
<body>
    <h1>
        Linking to GeeksforGeeks using JavaScript
    </h1>
      
    <h3>Using 'window.location.href' method</h3>
      
    <p>
        Click the button below to go
        to the GeeksforGeeks website:
    </p>
  
    <button onclick="goToGeeksforGeeks()">
        Go to GeeksforGeeks
    </button>
  
    <script>
        function goToGeeksforGeeks() {
            window.location.href = 
                "https://write.geeksforgeeks.org/";
        }
    </script>
</body>
  
</html>


Output:

 

Using window.location.assign() Method: This method is used to load a new document by setting the value of the current window’s location.href property. It is similar to setting window.location.href, but also allows you to use a history object to add a new entry to the browser’s history.

Approach: Using window.location.assign(): This approach is similar to setting window.location.href. The difference is that this method also allows you to navigate to a specific URL while preserving the current page’s history. This means that the back button will still work and take the user back to the previous page.

Syntax:

window.location.assign("https://www.gfg.com");

Example: This HTML code links with the GeeksforGeeks home page when the button is used.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        Linking to GeeksforGeeks 
        using JavaScript
    </title>
</head>
  
<body>
    <h1>
        Linking to GeeksforGeeks 
        using JavaScript
    </h1>
      
    <h3>Using 'window.location.assign' method</h3>
      
    <p>
        Click the button below to go to
        the GeeksforGeeks website:
    </p>
  
    <button onclick="goToGeeksforGeeks()">
        Go to GeeksforGeeks
    </button>
  
    <script>
        function goToGeeksforGeeks() {
            window.location.assign(
                "https://write.geeksforgeeks.org/");
        }
    </script>
</body>
  
</html>


Output:

 

Using window.location.replace() Method: This method is similar to window.location.assign(), but it does not create a new entry in the browser’s history. Instead, it replaces the current entry with the new one, effectively making it impossible to use the back button to return to the previous page

Approach: Using window.location.replace() Method: This approach also navigates to a specific URL but does not add an entry to the browser’s history. This means that the user cannot use the back button to navigate to the previous page.

Syntax:

window.location.replace("https://www.example.com");

Example: This HTML code links with the GeeksforGeeks home page when the button is used.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        Linking to GeeksforGeeks 
        using JavaScript
    </title>
</head>
  
<body>
    <h1>
        Linking to GeeksforGeeks 
        using JavaScript
    </h1>
      
    <h3>Using 'window.location.replace' method</h3>
      
    <p>
        Click the button below to go to
        the GeeksforGeeks website:
    </p>
  
    <button onclick="goToGeeksforGeeks()">
        Go to GeeksforGeeks
    </button>
  
    <script>
        function goToGeeksforGeeks() {
            window.location.replace(
                "https://write.geeksforgeeks.org/");
        }
    </script>
</body>
  
</html>


Output:

 

This method does not allow users to go back to the previous page, unlike the other two methods.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads