Javascript - 如何从location.href中删除域 [英] Javascript - how to remove domain from location.href
问题描述
我需要使用Javascript从location.href中删除域名。我有如下链接: http://localhost/App/User/UserOrder.aspx?id = 949abc91-a644-4a02-aebf-96da3ac7d8e1& type = MO
并且我需要要有没有 http:// localhost
的链接,并且将来没有它的真实域名。
我会在Javascript函数中使用这些trimed链接,所以我想用Javascript修剪它。
我试过了: window.location.href.split('/')[2];
但是我只能得到域的形式。我想摆脱域名。
这里的任何帮助都非常感谢! 使用 window.location.pathname
。这为您提供了相对于主机的路径。详情请参阅 。
<对于任何URL,假设变量
url
包含你的URL,你可以这样做: url = url.replace(/^.* \ / \ / [^ \ /] + /,'')
I need to remove the domain name from location.href using Javascript. I have links like: http://localhost/App/User/UserOrder.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1&type=MO
and I need to have links without http://localhost
and in future without it's real domain name.
I will use those trimed links in Javascript function so I would like to trim it also in Javascript.
I have tried: window.location.href.split('/')[2];
but I could only get domain form it. And I want to get rid of domain.
Any help here much appreciated!
Use window.location.pathname
. This gives you the path relative to the host. See here for more details.
For any arbitrary URL, assuming that the variable url
contains your URL, you can do:
url = url.replace(/^.*\/\/[^\/]+/, '')
这篇关于Javascript - 如何从location.href中删除域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!