Working with URI in JavaScript

2019-09-14 18:00发布

问题:

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?

回答1:

Try this:

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



回答2:

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