How to get the browser to navigate to URL in JavaScript?

 

You can use JavaScript to navigate the browser to a URL by setting the window.location.href property to the desired URL. Here's an example:

javascript
// Define the URL you want to navigate to var url = "https://www.example.com"; // Navigate to the URL window.location.href = url;

In this example:

  1. We define the URL we want to navigate to and store it in the url variable.

  2. We use window.location.href to set the browser's location to the specified URL by assigning the url variable to it.

When you run this JavaScript code in a web page, the browser will navigate to the specified URL (in this case, "https://www.example.com").

Comments