• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Page Details / How To Get The Current URL In JavaScript

How To Get The Current URL In JavaScript

How To Get The Current URL In JavaScript

JavaScript provides multiple ways to get the current URL of a webpage. In this article, we will discuss the most common methods to achieve this.

Method 1: Using the window.location object

The window.location object is a property of the window object and contains information about the current URL. The href property of this object returns the entire URL of the current page, including the protocol (http or https), domain, and path. Here’s an example:

console.log(window.location.href);

Method 2: Using the document.URL property

The document.URL property is another way to get the current URL of a webpage. It returns the entire URL, including the protocol, domain, and path. Here’s an example:

console.log(document.URL);

Method 3: Using the document.location.href property

This method also provides the entire URL, including the protocol, domain, and path. The document.location object is a property of the document object and contains information about the current URL. The href property of this object returns the entire URL.

console.log(document.location.href);

All of the above methods will give you the current URL of the webpage. You can choose any of these methods based on your requirement and use it in your JavaScript code.

Note: It is worth mentioning that you can also access other parts of the url like for example

console.log(window.location.hostname) // will return the hostname (www.example.com)
console.log(window.location.pathname) // will return the path '/about'
console.log(window.location.protocol) // will return the protocol 'http:'

and many other properties.

Page Details

Related Snippets:

  • Get the Width and Height of the Window
  • Return the current URL
  • Get Width and Height of an Element
  • Delayed Image Loader

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers