【问题标题】:How to get the pathname in JS after the /? [duplicate]如何在 / 之后获取 JS 中的路径名? [复制]
【发布时间】:2014-10-05 12:09:46
【问题描述】:

如果当前网址为:“https://stackoverflow.com/questions/ask

什么是获得的好方法:“questions/ask”?

【问题讨论】:

  • window.location.pathname
  • @RGraham 我想要 / 之后的路径名
  • 只需将其从字符串中删除即可。 (.substr(1);)
  • @Fataoulas 你真的试着找到自己吗?

标签: javascript


【解决方案1】:

你可以使用

window.location.pathname.substring(1)

【讨论】:

  • 这得到了起始 /。在此之后我想要一切
  • @Fataoulas 你说你想要没有域或协议的字符串。 / 不是域的一部分,它路径的一部分。
  • 只需将其从字符串中删除即可。 (.substr(1);)
【解决方案2】:

你会使用window.location.pathname

console.log(window.location.pathname.substring(1));

这将输出第一个 / 之后的所有内容

【讨论】:

    猜你喜欢
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 2020-03-14
    • 2017-05-20
    • 2011-12-26
    相关资源
    最近更新 更多