7

I have a website on a host example.myhost.com but I also have a sub-domain for French fr.example.myhost.com which is different than 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 do I get the URL typed in the address bar of the browser?

12
  • Do you see the right content if you run alert(location)? Commented Feb 5, 2012 at 22:31
  • location.href should give you the entire url. Commented Feb 5, 2012 at 22:33
  • @Douglas, no , the alert box gives example.myhost.com Commented Feb 5, 2012 at 22:33
  • @AlienWebguy, it doesn't give me the current url Commented Feb 5, 2012 at 22:34
  • 1
    @Alon what browser are you using? Commented Feb 5, 2012 at 22:34

3 Answers 3

15

window.location

The subdomain shouldn't make a difference. If, however, you've typed the URL in the address bar, after the page has loaded (but not hit enter), then that's a different story altogether.

Sign up to request clarification or add additional context in comments.

3 Comments

true, but the way he worded the question i'm assuming he actually has loaded the page with that url
In my browser window, I see fr.example.myhost.com while when I do window.location, I see example.myhost.com
do you have an actual webpage example?
2

That's weird, cause it works for me. You might try:

  • window.location.host
  • window.location.hostname
  • window.location.origin

Comments

2

Look the same question here : Get current URL in web browser

Use :

console.log(document.URL);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.