Get the Url of the Current Page Using JavaScript

Get the Url of the Current Page Using JavaScript

To get the URL of the page you're currently on, JavaScript offers the Location interface that is exposed by the global Window object by your browser.

Originally published by Nick Major at https://coderrocketfuel.com

To get the URL of the page you're currently on, JavaScript offers the Location interface that is exposed by the global Window object by your browser.

And you can access the Location interface with:

window.location

And since Window is a global object in the browser, you can access it using this also:

location

And if you want the href of the current page, use this:

location.href
// "https://coderrocketfuel.com/article/get-the-url-of-the-current-page-using-javascript"

There are also a ton of other useful properties in the location object.

Here are some examples for the page you are currently on:

location.protocol // "https:"
location.host     // "coderrocketfuel.com"
location.hostname // "coderrocketfuel.com"
location.port     // ""
location.pathname // "/article/get-the-url-of-the-current-page-using-javascript"
location.origin   // "https://coderrocketfuel.com"

You can read more about both the Window and Location interfaces on the Mozilla Developer Web Docs.

As always, thanks for reading and happy coding!

Thanks for reading

If you liked this post, please do share/like it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

The Complete JavaScript Course 2019: Build Real Projects!

Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

JavaScript Bootcamp - Build Real World Applications

The Web Developer Bootcamp

JavaScript Programming Tutorial - Full JavaScript Course for Beginners

New ES2019 Features Every JavaScript Developer Should Know

Best JavaScript Frameworks, Libraries and Tools to Use in 2019

JavaScript Basics Before You Learn React

Build a CMS with Laravel and Vue


javascript web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Web Developer

Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.