Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to get current url in JavaScript

Getting Started with URLs in JavaScript

URLs (Uniform Resource Locators), often simply referred to as web addresses, are a core part of the internet. They guide us to where we want to go, be it a blog post, a video, or a shopping site. To make your JavaScript application interact with URLs, you may want to get the current URL. This is done using the window.location object.

What is the window.location Object?

In JavaScript, the window object is like the control center of a browser. It holds global variables, functions, history, and, of course, location. When we talk about window.location, we're looking at an object that holds information about the current URL.

Getting the Current URL in JavaScript

To get the current URL in JavaScript, we can use window.location.href. This returns the entire URL. Here's an example.

console.log(window.location.href);

If you run this on your browser's console, it will print out the current URL. For example, if you're on 'https://www.example.com/', it will print 'https://www.example.com/'.

Breaking Down the URL

Now, what if you want to get specific components of the URL instead of the whole thing? The window.location object has several properties that can be used to access different parts of the URL. Here are some examples:

  • window.location.protocol returns the web protocol (http: or https:)
  • window.location.host returns the hostname and port (www.example.com)
  • window.location.pathname returns the path of the URL (/path/)
  • window.location.hash returns the anchor portion of the URL (#anchor)

Here's a simple example:

console.log(window.location.protocol); // Outputs: https:
console.log(window.location.host); // Outputs: www.example.com
console.log(window.location.pathname); // Outputs: /path/
console.log(window.location.hash); // Outputs: #anchor

Changing the Current URL

In addition to getting the current URL, the window.location object also allows you to change the URL. You can do this with the window.location.assign() method, which loads a new document.

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

This will redirect the browser to 'https://www.newwebsite.com'.

A Word of Caution

While using window.location can be very helpful, you should be careful not to manipulate the URL in a way that could confuse or mislead your users. Changing the URL without changing the content of the page can lead to a poor user experience.

Conclusion

URLs are like the addresses of the web. They guide users to the content they're looking for, and they're a crucial part of any web application. Understanding how to get and manipulate the current URL with JavaScript is an important skill for any web developer. It opens up a world of possibilities for creating dynamic, interactive web applications.

Next time you’re working on a web project, remember the power you hold with the window.location object. Like a digital compass, it can guide you and your users through the vast landscape of the internet. Happy coding!