有没有什么方法来获得无查询字符串的URL?有没有什么方法来获得无查询字符串的URL?(Is ther

2019-05-13 08:53发布

我有一个像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。

Answer 1:

试试这个: window.location.href.split('?')[0]



Answer 2:

阅读Window.locationLocation界面:

var url = [location.protocol, '//', location.host, location.pathname].join('');


Answer 3:

location.toString().replace(location.search, "")


Answer 4:

var url = window.location.origin + window.location.pathname;


Answer 5:

如果您也想删除哈希,试试这个: window.location.href.split(/[?#]/)[0]



Answer 6:

尝试:

document.location.protocol + '//' +
document.location.host +
document.location.pathname;

(注: .host而不是.hostname该端口将被列入过,如果需要的话)



Answer 7:

只是削减了使用字符串分割(简单的方法):

var myString = "http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235"
var mySplitResult = myString.split("?");
alert(mySplitResult[0]);


Answer 8:

要获得除查询网址的每一部分:

var url = (location.origin).concat(location.pathname).concat(location.hash);

请注意,这包括散列为好,如果有一个(我知道有一个在你的榜样URL没有散,但我为了保持完整性这方面)。 为了消除乱码,根本排除.concat(location.hash)

这是更好的做法是使用concat加入的JavaScript字符串连接在一起(而不是+ ):在某些情况下,它避免了问题,比如类型混淆。



Answer 9:

这里有两种方法:

<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>


Answer 10:

这个怎么样: location.href.slice(0, - ((location.search + location.hash).length))



Answer 11:

使用的特性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



文章来源: Is there any method to get the URL without query string?