I am using jQuery. How do I get the path of the current URL and assign it to a variable?
Example URL:
http://localhost/menuname.de?foo=bar&number=0
I am using jQuery. How do I get the path of the current URL and assign it to a variable? Example URL:
|
|||||||||||||||||
|
To get the path, you can use:
|
|||||||||||||||||||||
|
In pure jQuery style:
The location object also has other properties, like host, hash, protocol, and pathname. |
|||||||||||||||||||||
|
This will work only if you have jQuery. For example:
|
|||||||||
|
If you need the hash parameters present in the URL,
|
|||||||||
|
You'll want to use JavaScript's built-in |
||||
Just add this function in JavaScript, and it will return the absolute path of the current path.
I hope it works for you. |
||||
|
This is a more complicated issue than many may think. Several browsers support built-in JavaScript location objects and associated parameters/methods accessible through So, if you have jQuery available and loaded, you might as well use jQuery (location), as the others mentioned because it resolves these issues. If however, you are doing-for an example-some client-side geolocation redirection via JavaScript (that is, using Google Maps API and location object methods), then you may not want to load the entire jQuery library and write your conditional code that checks every version of Internet Explorer/Firefox/etc. Internet Explorer makes the front-end coding cat unhappy, but jQuery is a plate of milk. |
|||||
|
For the host name only, use:
|
||||
|
This will also work:
|
|||||
|
window.location is an object in javascript. it returns following data
in jquery you can use
|
|||||||||
|
You can log window.location and see all the options, for just the URL use:
for the whole path use:
there's also location.__
|
|||||
|
If there is someone who wants to concatenate the URL and hash tag, combine two functions:
|
|||||
|
This will return the absolute URL of the current page using JavaScript/jQuery.
|
||||
|
I have this to strip out the GET variables.
|
|||
|
To get the URL of the parent window from within an iframe:
NB: only works on same domain |
||||
|
|
|||
|
The following are examples of useful code snippets that can be used – some of the examples use standard JavaScript functions and are not specific to jQuery: |
||||
|
Here is an example to get the current URL using jQuery and JavaScript:
|
||||
|
Use window.location.href. This will give you the complete URL. |
||||
|
window.location will give you the current URL, and you can extract whatever you want from it... |
||||
|
If you want to get the path of the root site, use this:
|
|||||||||
|
|
||||
|
See purl.js. This will really help and can also be used, depending on jQuery. Use it like this:
|
||||
|
The Simple Way:
Another Way:
|
|||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?