How to Get Browser to Navigate URL in JavaScript?
Last Updated :
29 Jul, 2024
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() Method:
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.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
11 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read