Easy Tutorial
❮ Android Tutorial Gridlayout Android Tutorial Fragment Demo4 ❯

JS Get Current Page URL

Category Programming Technology

In the process of WEB development, we often need to obtain the current page's URL. This section introduces how to get the current page's URL using JavaScript.

To get the current page's URL with JS, you can use the following code:

window.location.href

This retrieves the complete URL.

Example

window.location.href

We can also obtain specific parts of the URL:

Example

var url      = window.location.href;  // Returns the full URL (https://www.tutorialpro.org/html/html-tutorial.html)
var pathname = window.location.pathname; // Returns the path part (/html/html-tutorial.html)
var origin   = window.location.origin;   // Returns the base URL (https://www.tutorialpro.org/)
var domain = document.domain;  // Returns the domain part (www.tutorialpro.org)

Reference Content

-Location Object

-HTML DOM Document Object

-JavaScript Window Location

-HTML domain Property

-JS Get URL Parameters

** Click to Share Notes

Cancel

-

-

-

❮ Android Tutorial Gridlayout Android Tutorial Fragment Demo4 ❯