javascript - Get current URL with jQuery?

ID : 457

viewed : 321

Tags : javascriptjqueryurljavascript

Top 5 Answer for javascript - Get current URL with jQuery?

vote vote


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 ( var origin   = window.location.origin;   // Returns base URL ( 
vote vote


In pure jQuery style:


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

vote vote

70  Property    Result ------------------------------------------ host hostname port        8082 protocol    http: pathname    index.php href hash        #tab2 search      ?foo=789  var x = $(location).attr('<property>'); 

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

<html> <script src=""></script> <script>   $(location).attr('href');      //   $(location).attr('pathname');  // index.php </script> </html> 
vote vote


If you need the hash parameters present in the URL, window.location.href may be a better choice.

window.location.pathname => /search  window.location.href   => 
vote vote


You'll want to use JavaScript's built-in window.location object.

Top 3 video Explaining javascript - Get current URL with jQuery?