How to get current url path in javascript?

In javascript, to get the path, you can use:


var pathname = window.location.pathname; 
// Returns path only : "check-path-example"
var url = window.location.href;
// Returns full URL : ''
var origin = window.location.origin;
// Returns base URL : ""

Or jquery, you can use:

// Returns host
// Returns href
// Returns protocol

The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page.

Add a Comment

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