【问题标题】:Pull out part of the page URL拉出部分页面URL
【发布时间】:2012-03-01 01:23:38
【问题描述】:

我的页面网址是这样的:

http://www.mydomain.com/directory/thisismy_page.html

使用 JavaScript,我想提取页面文件名中下划线之前的部分。所以在这个例子中,我想要:

thisismy

我目前有这个:

var filename = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);

但这会引出thisismy_page.html。如何在下划线处“停止”它,所以我得到的只是thisismy

【问题讨论】:

    标签: javascript url path location substring


    【解决方案1】:
    filename=filename.split("_")[0];
    

    这会在下划线处拆分它并取第一部分(或第 0 部分)。

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 2012-07-30
      • 2021-05-15
      • 2017-02-05
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多