我有一个像URL http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235
。
我想没有查询字符串的网址: http://localhost/dms/mduserSecurity/UIL/index.php
。
有没有在JavaScript中这方面的任何方法? 目前我使用document.location.href
,但它返回完整的URL。
我有一个像URL http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235
。
我想没有查询字符串的网址: http://localhost/dms/mduserSecurity/UIL/index.php
。
有没有在JavaScript中这方面的任何方法? 目前我使用document.location.href
,但它返回完整的URL。
试试这个: window.location.href.split('?')[0]
阅读Window.location
和Location
界面:
var url = [location.protocol, '//', location.host, location.pathname].join('');
location.toString().replace(location.search, "")
var url = window.location.origin + window.location.pathname;
如果您也想删除哈希,试试这个: window.location.href.split(/[?#]/)[0]
尝试:
document.location.protocol + '//' +
document.location.host +
document.location.pathname;
(注: .host
而不是.hostname
该端口将被列入过,如果需要的话)
只是削减了使用字符串分割(简单的方法):
var myString = "http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235"
var mySplitResult = myString.split("?");
alert(mySplitResult[0]);
要获得除查询网址的每一部分:
var url = (location.origin).concat(location.pathname).concat(location.hash);
请注意,这包括散列为好,如果有一个(我知道有一个在你的榜样URL没有散,但我为了保持完整性这方面)。 为了消除乱码,根本排除.concat(location.hash)
这是更好的做法是使用concat
加入的JavaScript字符串连接在一起(而不是+
):在某些情况下,它避免了问题,比如类型混淆。
这里有两种方法:
<script type="text/javascript">
var s="http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu
=true&pcode=1235";
var st=s.substring(0, s.indexOf("?"));
alert(st);
alert(s.replace(/\?.*/,''));
</script>
这个怎么样: location.href.slice(0, - ((location.search + location.hash).length))
使用的特性window.location
var loc = window.location;
var withoutQuery = loc.hostname + loc.pathname;
var includingProtocol = loc.protocol + "//" + loc.hostname + loc.pathname;
你可以看到在多个属性https://developer.mozilla.org/en/DOM/window.location