In WEB development, we often need to get the current page's URL. This chapter introduces how to get the current page's URL with JavaScript.
JS can use the following code to get the current page's URL:
window.location.href
It can get the complete URL.
We can also get a part of the URL:
Example
var url = window.location.href; // Return the full URL (https://www.example.com/html/html-tutorial.html)
var pathname = window.location.pathname; // Return the path part (/html/html-tutorial.html)
var origin = window.location.origin; // Return the base URL (https://www.example.com/)
var domain = document.domain; // Return the domain name part (www.example.com)
var pathname = window.location.pathname; // Return the path part (/html/html-tutorial.html)
var origin = window.location.origin; // Return the base URL (https://www.example.com/)
var domain = document.domain; // Return the domain name part (www.example.com)
Try it Yourself »