Get current URL with jQuery? Get current URL with jQuery? javascript javascript

Get current URL with jQuery?


To get the path, you can use:

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


In pure jQuery style:

$(location).attr('href');

The location object also has other properties, like host, hash, protocol, and pathname.


http://www.refulz.com:8082/index.php#tab2?foo=789Property    Result------------------------------------------host        www.refulz.com:8082hostname    www.refulz.comport        8082protocol    http:pathname    index.phphref        http://www.refulz.com:8082/index.php#tab2hash        #tab2search      ?foo=789var x = $(location).attr('<property>');

This will work only if you have jQuery. For example:

<html><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script><script>  $(location).attr('href');      // http://www.refulz.com:8082/index.php#tab2  $(location).attr('pathname');  // index.php</script></html>