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&nu
// get current URL
$(location).attr('href');
var pathname = window.location.pathname;
alert(window.location);
In jstl we can access current url path using pageContext.request.contextPath
, If you want to do a ajax call,
url = "${pageContext.request.contextPath}" + "/controller/path"
Ex: in the page http://stackoverflow.com/questions/406192
this will give http://stackoverflow.com/controller/path
var newURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
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)
This will return the absolute URL of the current page using JavaScript/jQuery.
document.URL
$("*").context.baseURI
location.href
Use window.location.href. This will give you the complete URL.