How to get current page URL with javascript?

If you are writing some javascript and need to get current current page url, the following code is useful. This is just give an idea to new javascript learners. If you need help, comments below.

 var origin   = window.location.origin;   
// Returns base URL (https://example.com)
 var url      = window.location.href;     
// Returns full URL (https://example.com/path/example.html) 
var pathname = window.location.pathname; 
// Returns path only (/path/example.html)

Demo:

Leave a Reply

Your email address will not be published. Required fields are marked *