How to get current browser url using javascript?

I have a website on the host example.myhost.com but I also have a subdomain for the French fr.example.myhost.com , which is different from example.myhost.com.

If I'm at fr.example.myhost.com and I use window.location.href , I get example.myhost.com instead of fr.example.myhost.com .

How to get the URL specified in the address bar of the browser?

+4
source share
3 answers

window.location

The subdomain should not matter. If, however, you typed the URL in the address bar after the page loaded (but did not press the enter button), then this is a completely different story.

+9
source

This is weird because it works for me. You can try:

  • window.location.host
  • window.location.hostname
  • window.location.origin
+3
source

See the same question: Get current url in web browser

Using:

 console.log(document.URL); 
0
source

Source: https://habr.com/ru/post/1394918/


All Articles