首页 > 解决方案 > 从javascript中的url路径中提取参数

问题描述

我想提取 url 路径的一部分,但我不知道该怎么做。我想从这样的路径中获取一个值(3252):

/forums/0-3252-1-1-my-topic-title.htm

我怎样才能做到这一点 ?

谢谢

标签: javascript

解决方案


如果你想使用 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>

点击链接阅读更多:窗口位置


推荐阅读