I am using jQuery. I'd like to 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. I'd like to get the path of the current URL and assign it to a variable.
Example URL:
http://localhost/menuname.de?foo=bar&number=0"
$(document).ready(function() {
// to show it in an alert window
alert(window.location);
// to store it in a variable
var loc = window.location;
});
window.location is standard JavaScript and does not require jQuery.
You'll want to use javascript's built-in window.location
object:
To get the path, you can use window.location.pathname:
$(document).ready(function() {
var pathname = window.location.pathname;
});
There is also a plugin: http://projects.allmarkedup.com/jquery_url_parser/index.php
If you need the hash parameters present in the URL, window.location.href may be a better chose.
window.location.pathname => /search
window.location.href => www.website.com/search#race_type=1
Just add this function in javascript and it will return the absolute path of the current path
function getAbsolutePath() {
var loc = window.location;
var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
return loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));
}
Hope it works for you
In pure jQuery style :
$(location).attr('href');
The location object has also other properties like host, hash, protocol, pathname, etc.
Try this jquery plugin http://allmarkedup.com/journal/2008/08/jquery-url-parser-v10/