How to Get Portion of URL Path in JavaScript

  1663 views   2 years ago Javascript

Use the window.location Object

You can use the built-in window.location object to retrieve the different part or portion of URL path in JavaScript, as demonstrated in the following example:

// If URL is
window.location.href //
window.location.protocol // http: // (includes port if there is one e.g. 3000)
window.location.hostname //
window.location.port // (provide port if there is one, otherwise empty string)
window.location.pathname // /products/search.php // ?q=ipad
window.location.hash // #featured


Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]