Get current URL with jQuery
Below tutorial explains,how to get the current URL of the page in JavaScript or JQUERY. The current URL represents a full page URL. We can also get current domain, hostname, pathname, protocol, port, hash and other components of the full page URL. You can also redirect the current page URL to other addresses with the properties of window.location
object.
Methods to get different components of the URL
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
We take a sample URL URL to understand the components of a URL. The current URL comprises the following components.
Example URL : http://www.getsourcecodes.com:8082/index.php#tab2?foo=789
host www. getsourcecodes .com:8082
hostname www. getsourcecodes .com
port 8082
protocol http:
pathname index.php
href http://www. getsourcecodes .com:8082/index.php#tab2
hash #tab2
search ?foo=789
Usage :
1 |
var x = $(location).attr('<Property>'); // like host,href,protocol etc... |