I'm Rituraj, a Developer @Nagarro. founder of Maddyzone. JavaScript, Angular, Node.

Buy Me a Coffee at ko-fi.com

How to get current url in JavaScript and jQuery ?

The [window.location](https://developer.mozilla.org/en-US/docs/Web/API/window.location?redirectlocale=en-US&redirectslug=DOM%2Fwindow.location) read-only property returns a Location object with information about the current location of the document.

we see all the property by this below Url

http://www.test.com:8082/index.php#tab2?foo=123 first  use window.location then write following property like

Property                                          Result

--------------------------------------------------------------------------
window.location.host                   www.test.com:8082
window.location.hostname               www.test.com
window.location.port                   8082
window.location.protocol               http
window.location.pathname               index.php
window.location.href                   http://www.test.com:8082/index.php#tab2
window.location.hash                   #tab2
window.location.search                 ?foo=123

Or if you have jQuery you can do it like this:

Property                                          Result

--------------------------------------------------------------------------
$(location).attr('host');                        www.test.com:8082
$(location).attr('hostname');                    www.test.com
$(location).attr('port');                        8082
$(location).attr('protocol');                    http
$(location).attr('pathname');                    index.php
$(location).attr('href');                        http://www.test.com:8082/index.php#tab2
$(location).attr('hash');                       #tab2
$(location).attr('search');                     ?foo=123

Rituraj Ratan

I'm Rituraj, a Developer @Nagarro. Founder of Maddyzone .