我有一个类似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。
我有一个类似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。
当前回答
只需将这两行添加到$(document)。在JS中准备如下:
$(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”和“/account/roles”)
它会激活它们两个。
其他回答
下面是一个使用URL()接口的方法:
new URL(location.pathname, location.href).href
这个怎么样:location。href。切片(0,- (location。Search + location.hash).length))
var url = window.location.origin + window.location.pathname;
Try:
document.location.protocol + '//' +
document.location.host +
document.location.pathname;
(注意:.host而不是.hostname,这样端口也包括在内,如果需要的话)
只需将这两行添加到$(document)。在JS中准备如下:
$(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”和“/account/roles”)
它会激活它们两个。