我有一个类似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。
当前回答
你可以创建一个URL实例,然后清除查询字符串:
const url = new URL(document.location.href);
url.search = '';
console.log(url.href);
其他回答
试试这个:
让path = window.location. hrf .split('?')[0] console.log ({path})
下面是一个使用URL()接口的方法:
new URL(location.pathname, location.href).href
只需使用split(简单的方法)切割字符串:
var myString = "http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235"
var mySplitResult = myString.split("?");
alert(mySplitResult[0]);
var url = window.location.origin + window.location.pathname;
如果你也想删除哈希,试试这个:window.location.href.split(/[?#]/)[0]