How to Get Current URL in jQuery

Sundeep Dawadi Feb 26, 2025 jQuery
  1. Method 1: Using window.location.href
  2. Method 2: Using document.URL
  3. Method 3: Extracting Components of the URL
  4. Conclusion
  5. FAQ
How to Get Current URL in jQuery

When working with web applications, knowing how to access the current URL can be incredibly useful. Whether you want to manipulate the URL for routing, analytics, or simply to display it, jQuery makes this task straightforward.

In this tutorial, we’ll explore different methods to retrieve the current URL using jQuery. By the end, you’ll be equipped with practical code snippets that you can easily integrate into your projects. So, let’s dive in and learn how to get the current URL in jQuery!

Method 1: Using window.location.href

One of the simplest ways to get the current URL in jQuery is by utilizing the window.location.href property. This property returns the entire URL of the current page, including the protocol (http or https), hostname, port, and path.

Here’s a quick example:

$(document).ready(function() {
    var currentUrl = window.location.href;
    console.log(currentUrl);
});

Output:

https://www.example.com/current-page

In this code snippet, we use jQuery’s $(document).ready() function to ensure that the DOM is fully loaded before executing our script. The window.location.href property captures the complete URL of the page, which we then log to the console. This method is straightforward and works in all modern browsers. It’s particularly useful when you need the full URL for redirection or logging purposes.

Method 2: Using document.URL

Another effective way to retrieve the current URL is by using document.URL. This property provides the same output as window.location.href, but it’s a bit more concise.

Here’s how you can implement it:

$(document).ready(function() {
    var currentUrl = document.URL;
    console.log(currentUrl);
});

Output:

https://www.example.com/current-page

In this example, we again use $(document).ready() to ensure our code runs after the DOM is ready. By assigning document.URL to the currentUrl variable, we capture the current page’s URL. This method is equally effective and can be used interchangeably with window.location.href. The beauty of using document.URL lies in its simplicity, making your code cleaner and easier to read.

Method 3: Extracting Components of the URL

Sometimes, you may not need the entire URL but rather specific components like the hostname, pathname, or search parameters. jQuery can help you extract these components using the window.location object.

Here’s an example demonstrating how to get different parts of the URL:

$(document).ready(function() {
    var protocol = window.location.protocol;
    var host = window.location.host;
    var pathname = window.location.pathname;
    var search = window.location.search;

    console.log('Protocol: ' + protocol);
    console.log('Host: ' + host);
    console.log('Pathname: ' + pathname);
    console.log('Search: ' + search);
});

Output:

Protocol: https:
Host: www.example.com
Pathname: /current-page
Search: ?query=example

In this code snippet, we extract various components of the current URL using the window.location object. The protocol, host, pathname, and search variables hold the respective parts of the URL. This method is beneficial when you need to manipulate or analyze specific sections of the URL, such as for query string processing or for rendering dynamic content based on the URL structure.

Conclusion

Retrieving the current URL in jQuery is a fundamental skill that can enhance your web development projects. By utilizing methods like window.location.href, document.URL, and the window.location object, you can easily access the complete URL or its individual components. These techniques not only simplify your coding process but also allow for greater flexibility in how you handle URLs within your applications. So go ahead and implement these methods in your next project, and watch how they streamline your workflow!

FAQ

  1. How can I get just the pathname of the current URL?
    You can use window.location.pathname to retrieve only the pathname part of the URL.

  2. Is it necessary to use jQuery to get the current URL?
    No, you can use plain JavaScript as well, but jQuery simplifies the process and enhances cross-browser compatibility.

  3. Can I capture the URL when a user navigates to a new page?
    Yes, you can use the popstate event to capture the URL when users navigate through history.

  4. What if I need to get the URL parameters?
    You can access the URL parameters using window.location.search and then parse them as needed.

  5. Are there any browser compatibility issues with these methods?
    No, these methods are widely supported in all modern browsers, making them reliable for web development.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe