Javascript 有没有什么方法可以在没有查询字符串的情况下获取 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5817505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any method to get the URL without query string?
提问by saint
I have a URL like http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235.
我有一个像http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235.
I want to get the URL without the query string: http://localhost/dms/mduserSecurity/UIL/index.php.
我想获取没有查询字符串的 URL: http://localhost/dms/mduserSecurity/UIL/index.php。
Is there any method for this in JavaScript? Currently I am using document.location.href, but it returns the complete URL.
在 JavaScript 中有任何方法吗?目前我正在使用document.location.href,但它返回完整的 URL。
回答by Felix Kling
Read about Window.locationand the Locationinterface:
阅读有关Window.location和Location界面:
var url = [location.protocol, '//', location.host, location.pathname].join('');
回答by tradyblix
Try this: window.location.href.split('?')[0]
尝试这个: window.location.href.split('?')[0]
回答by Quentin
location.toString().replace(location.search, "")
回答by Jason
var url = window.location.origin + window.location.pathname;
回答by user1079877
If you also want to remove hash, try this one: window.location.href.split(/[?#]/)[0]
如果您还想删除哈希,请尝试以下操作: window.location.href.split(/[?#]/)[0]
回答by s4y
Here's an approach using the URL() interface:
这是使用URL() 接口的方法:
new URL(location.pathname, location.href).href
回答by Alnitak
Try:
尝试:
document.location.protocol + '//' +
document.location.host +
document.location.pathname;
(NB: .hostrather than .hostnameso that the port gets included too, if necessary)
(注意:.host而不是.hostname在必要时也包括端口)
回答by pleasedontbelong
just cut the string using split (the easy way):
只需使用 split 剪断字符串(简单的方法):
var myString = "http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235"
var mySplitResult = myString.split("?");
alert(mySplitResult[0]);
回答by Andrew Faulkner
To get every part of the URL except for the query:
要获取除查询之外的 URL 的每个部分:
var url = (location.origin).concat(location.pathname).concat(location.hash);
Note that this includes the hash as well, if there is one (I'm aware there's no hash in your example URL, but I included that aspect for completeness). To eliminate the hash, simply exclude .concat(location.hash).
请注意,这也包括散列,如果有的话(我知道您的示例 URL 中没有散列,但为了完整起见,我包含了该方面)。要消除散列,只需 exclude .concat(location.hash)。
It's better practice to use concatto join Javascript strings together (rather than +): in some situations it avoids problems such as type confusion.
使用concat将 Javascript 字符串连接在一起(而不是+)是更好的做法:在某些情况下,它可以避免诸如类型混淆之类的问题。
回答by LI XiangChen
How about this: location.href.slice(0, - ((location.search + location.hash).length))
这个怎么样: location.href.slice(0, - ((location.search + location.hash).length))

