Net-informations.com

How to get current URL with JavaScript?

window.location.href

JavaScript provides you with different methods to get and set the current URL , which is displayed in the web-browser's address bar. All these methods use the Location object , which is a property of the Window object. Use its properties to get information on the current page address or use its methods to do some page redirect or refresh.

run this source code Try it Yourself

URL of this page is:

Get current url | Full Source | javascript

URL structure

URL's are also called links. It is an address on the internet. It's made up of a protocol , domain name, and a path.


Get Current URL and Components with JavaScript (Protocol, Domain, Port, Path, Query, Hash)

JavaScript window.location properties

Using these window.location object properties you can access all of these URL components and what they can set or return:

  1. href - Full URL (http://www.example.com:8082/index.php#tab2)
  2. protocol - Protocol Schema (http: or https)
  3. host - Domain name + port (www.example.com:8082)
  4. hostname - Domain name (www.example.com)
  5. port - Port Number (8082)
  6. pathname - the path name of the URL (index.php)
  7. search - ? followed by Query String (?foo=789)
  8. hash - # followed by the Anchor or Fragment identifier (#tab2)
run this source code Try it Yourself

window.location properties in javascript

window.location properties| FullSource | JavaScript










net-informations.com (C) 2022    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About