Working with URI in JavaScript

北战南征 提交于 2020-02-08 05:11:47

问题


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


来源:https://stackoverflow.com/questions/8547709/working-with-uri-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!