javascript - 从javascript中的url路径中提取参数
问题描述
我想提取 url 路径的一部分,但我不知道该怎么做。我想从这样的路径中获取一个值(3252):
/forums/0-3252-1-1-my-topic-title.htm
我怎样才能做到这一点 ?
谢谢
解决方案
如果你想使用 URL,有一个强大的工具可以用来回复,那就是JavaScript Window Location。
该window.location
对象可用于获取当前页面地址(URL)。
window.location 对象有很多属性或方法。
一些例子:
window.location.href
返回当前页面的 href (URL)window.location.hostname
返回网络主机的域名window.location.pathname
返回当前页面的路径和文件名window.location.protocol
返回使用的网络协议(http: 或 https:)window.location.assign()
加载一个新文档
例子:
显示当前页面的 href(URL):
<script>
alert(window.location.href);
</script>
点击链接阅读更多:窗口位置
推荐阅读
- node.js - 无法将项目推送到 Heroku
- javascript - Jqgrid edittype : select editoptions: dataUrl 没有被调用来填充数据
- javascript - 如何使用 useRef 挂钩访问数据属性值?
- python - 在精神病学中使用顺序 ITI 随机化图像呈现
- swift - Localizable.strings 在列表中不起作用
- python - CMakeLists.txt:2 处的 CMake 错误
: 找不到 CMAKE_Fortran_COMPILER - python - Pandas:遍历行,添加和减去日期,根据行值将这些日期附加到新列
- qt - Qt.createComponent() 在动态绑定时会导致后面的“Object has a QQmlContext”错误
- c - C 程序 - 如何将 char* 数组的值分配给 2d char* 数组?
- python - python websocket-client握手状态400错误请求