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?

Christofer Eliasson
34k7 gold badges77 silver badges103 bronze badges
asked Feb 5, 2012 at 22:28
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.

answered Feb 5, 2012 at 22:36
3
  • true, but the way he worded the question i'm assuming he actually has loaded the page with that url Commented Feb 5, 2012 at 22:41
  • In my browser window, I see fr.example.myhost.com while when I do window.location, I see example.myhost.com Commented Feb 5, 2012 at 22:41
  • do you have an actual webpage example? Commented Feb 5, 2012 at 22:53
2

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

  • window.location.host
  • window.location.hostname
  • window.location.origin
answered Feb 5, 2012 at 22:34
2

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

Use :

console.log(document.URL);
answered Sep 2, 2013 at 13:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.