0

I have a URL like below :

http://localhost:1692/fa/Help.aspx#Help

the code below returns that :

    alert(document.URL);

I only want to get the http://localhost:1692 part of this URL.

How can I get it with jQuery or JavaScript?

5
  • alert(window.location.host) Commented Dec 17, 2011 at 20:30
  • my problem is about that port...some url s like this local url have that port. Commented Dec 17, 2011 at 20:31
  • ok, so sorry -> alert(window.location.host) is my answer. Commented Dec 17, 2011 at 20:33
  • @steveax plz put your comment as answer. Commented Dec 17, 2011 at 20:37
  • If you want the protocol too, then Esailija's answer is the ticket. MDN on window.location Commented Dec 17, 2011 at 20:47

2 Answers 2

4

Try this:

alert( location.protocol + "//" + location.host )

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

Comments

0
location.protocol + '//' + location.host

And, if you want, you have these other properties:

  • hash
  • search - url querystring
  • hostname - only the hostname (without port)
  • port - only the port number
  • pathname - the local url

Comments

Your Answer

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.