我有一个类似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。
当前回答
这个怎么样:location。href。切片(0,- (location。Search + location.hash).length))
其他回答
阅读Window。location和location接口:
const urlPieces = [location.]协议,'//',位置。主机,location.pathname] let url = urlPieces.join(") console.log ({urlPieces、url})
location.toString().replace(location.search, "")
这个怎么样:location。href。切片(0,- (location。Search + location.hash).length))
如果你也想删除哈希,试试这个:window.location.href.split(/[?#]/)[0]
试试这个:
让path = window.location. hrf .split('?')[0] console.log ({path})