jQuery code to get the current web page full URL and store it in a variable for use with other scripts. This is the URL we see in your address bar.
javascript code
$(document).ready(function()
{
//jquery
$(location).attr('href');
//pure javascript
var pathname = window.location.pathname;
// to show it in an alert window
alert(window.location);
});
There is also this function that may help when determining absolute paths.
javascript code
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));
}
window.location is an object in javascript.
It returns following data:-
javascript code
window.location.host #returns host
window.location.hostname #returns hostname
window.location.path #return path
window.location.href #returns full current url
window.location.port #returns the port
window.location.protocol #returns the protocol
Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.
Add Comment