有没有办法在没有查询字符串的情况下获取URL?

2022-08-29 23:51:13

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

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

在JavaScript中有什么方法可以做到这一点吗?目前我正在使用,但它返回完整的URL。document.location.href


答案 1

试试这个:

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

答案 2

阅读 Window.locationLocation 界面:

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

console.log({urlPieces, url})