我有一个类似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。
当前回答
Try:
document.location.protocol + '//' +
document.location.host +
document.location.pathname;
(注意:.host而不是.hostname,这样端口也包括在内,如果需要的话)
其他回答
你可以创建一个URL实例,然后清除查询字符串:
const url = new URL(document.location.href);
url.search = '';
console.log(url.href);
Try:
document.location.protocol + '//' +
document.location.host +
document.location.pathname;
(注意:.host而不是.hostname,这样端口也包括在内,如果需要的话)
你可以像这样使用URL构造函数:
const href = 'http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235';/ / document.location.href const url =新的url (href); const noSearchUrl = href.replace(url。”); console.log (noSearchUrl);
location.toString().replace(location.search, "")
这个怎么样:location。href。切片(0,- (location。Search + location.hash).length))