These answers are good. For the sake of others who came here later and want a fully encapsulated answer, I thought I would put together a function
function locationHREFWithoutResource() { var pathWORes = location.pathname.substring(0, location.pathname.lastIndexOf("/")+1); var protoWDom = location.href.substr(0, location.href.indexOf("/", 8)); return protoWDom + pathWORes; };
This will return the entire URL (href) to the last directory, including the trailing slash. I tested it at several URLs, visiting sites and dropping a function in the console, and then calling it. Example and results:
http://meyerweb.com/eric/tools/dencoder/ -> "http://meyerweb.com/eric/tools/dencoder/" https://www.google.com/ -> "https://www.google.com/"` http://stackoverflow.com/questions/16417791/how-to-get-current-url-without-page-in-javascript-or-jquery -> "http://stackoverflow.com/questions/16417791/"
The last page is the page.
source share