是否有任何方法可以在没有查询字符串的情况下获取URL

Is there any method to get the URL without query string?

本文关键字:字符串 情况下 获取 URL 查询 方法 任何 是否      更新时间:2023-09-26

我有一个类似http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235的URL。

我想获得没有查询字符串的URL: http://localhost/dms/mduserSecurity/UIL/index.php

在JavaScript中有任何方法吗?目前我正在使用document.location.href,但它返回完整的URL。

试试这个:

let path = window.location.href.split('?')[0]
console.log({path})

了解Window.locationLocation接口:

const urlPieces = [location.protocol, '//', location.host, location.pathname]
let url = urlPieces.join('')
console.log({urlPieces, url})

location.toString().replace(location.search, "")
var url = window.location.origin + window.location.pathname;

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

下面是使用URL()接口的方法:

new URL(location.pathname, location.href).href

尝试:

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

(注意:.host而不是.hostname,以便端口也包括在内,如果必要的话)

获取URL的所有部分,除了查询:

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

注意,如果有哈希的话,这也包括哈希(我知道示例URL中没有哈希,但为了完整性,我包括了这个方面)。要消除散列,只需排除.concat(location.hash)

使用concat将Javascript字符串连接在一起(而不是+)是更好的实践:在某些情况下,它可以避免类型混淆等问题。

使用split(最简单的方法)剪切字符串:

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

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

查看更多属性

location.href.slice(0, - ((location.search + location.hash).length))

有两种方法:

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

您可以像这样使用URL构造函数:

const href = 'http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235'; // document.location.href
const url = new URL(href);
const noSearchUrl = href.replace(url.search, '');
console.log(noSearchUrl);

您可以创建URL的实例,然后清除查询字符串:

const url = new URL(document.location.href);
url.search = '';
console.log(url.href);

将这两行添加到$(document)中。

$(document).ready(function () {
 $("div.sidebar nav a").removeClass("active");
        $('nav a[href$="'+ window.location.pathname.split("?")[0] +'"]').addClass('active');
});

最好使用美元符号($)(以)

$('nav a[href$

代替(^)(以)开头

$('nav a[href^

因为,如果你使用(^)符号并且你在导航菜单中嵌套了url,(例如"/account"answers"/account/roles")

window.location.href.split("#")[0].split("?")[0]

2023年我会做什么:

const windowUrl = window.location.href;
const { origin, pathname } = new URL(windowUrl);
const urlWithoutQueryOrHash = `${origin}${pathname}`;

如果您正在使用导航栏,并希望在单击侧边栏导航后获得纯url,那么以下代码可能会有所帮助:

$(document).ready(function () {
    $("div.sidebar nav a").removeClass("active");
    var urlPath = window.location.pathname.split("?")[0];
    var nav = $('div.sidebar nav a').filter(function () {
        return $(this).attr('href').toLowerCase().indexOf(urlPath.toLocaleLowerCase()) > -1;
    });
    $(nav).each(function () {
        if ($(this).attr("href").toLowerCase() == urlPath.toLocaleLowerCase())
            $(this).addClass('active');
    });
});
相关文章: