使用javascript从URL获取路径和查询字符串
Get path and query string from URL using javascript
我有这个:
http://127.0.0.1:8000/found-locations/?state=--&km=km
我想要这个:
found-locations/?state=--&km=km
如何在javascript中做到这一点?
我尝试了window.location.href
,但它给了我整个url
我试过window.location.pathname.substr(1)
,但它给了我found-locations/
使用location.pathname
和location.search
:
(location.pathname+location.search).substr(1)
window.location.pathname + window.location.search
将获得基本url /found-locations
加上查询字符串?state=--&km=km
如果您的url是字符串,则可以创建URL
对象并使用pathname
和search
属性。
let strurl = 'http://www.test.com/param1/param2?test=abc';
let url = new URL(strurl)
let pathandQuery = url.pathname + url.search;
let strurl = 'http://www.test.com/param1/param2?test=abc';
let url = new URL(strurl)
let pathandQuery = url.pathname + url.search;
console.log(pathandQuery);
获取所有路径、查询甚至散列:location.href.replace(location.origin, '')
URI.js是一个很好的解析URI的JavaScript库。它可以用流利的语法完成您的要求。
相关文章:
- 如何有效地将游戏数据存储在URL查询字符串中
- 从查询字符串参数推断出正确的数据类型
- AngularJS:获取链接中没有哈希的查询字符串值
- AngularJS,如何更改查询字符串
- 如何将activeadmin查询字符串传递给batch_action
- 如何从查询字符串中的输入字段发回文本
- Django在使用AJAX时没有接收到新的查询字符串
- 使用查询字符串方法清除浏览器缓存
- 用于刷新的Javascript更改查询字符串
- 我想将查询字符串变量添加到JSON名称/值对中
- 将查询字符串值从页面传递 asp.net 到 iframe
- jqueryajax生成查询字符串,如果未定义则不通过
- 如何将查询字符串传递到HTML框架
- AngularJs将查询字符串发布到c#控制器
- Jquery - 从查询字符串中获取“#”值之后,而不是在“?”符号值之后获取
- 在 JavaScript 中将查询字符串传递给 Web 服务 (ASP.NET)
- 在查询字符串中查找 where 子句
- POST encodeURI查询字符串内部服务器错误 500
- JavaScript:从具有使用 # 符号的路由的 URL 获取查询字符串
- 更改链接 URL 中的路径名,同时保持查询字符串不变