如何根据当前URL将文本更改为其他内容

How to change text to something else based on the current URL?

本文关键字:其他 文本 何根 URL      更新时间:2023-09-26

我正在一个商店网站上工作,希望每个页面上显示的收藏标题都能显示为基本的面包屑,比如"WOMEN>JEWELLY"。我遇到的问题是,每个收藏页面上唯一显示的是"女人"。

我想根据当前页面的URL将此文本更改为基本的breadcrumb。例如,主要女性系列的URL为http://www.shopfashionworthy.com/collections/women,但个人女性珠宝产品的URL为http://www.shopfashionworthy.com/collections/women/jewelry.我想用某种方式说"如果URL是http://www.shopfashionworthy.com/collections/women/jewelry,将收藏标题更改为"女士>珠宝"。希望我能理解。

收藏标题的当前代码为:

<!-- START EXCERPT -->
<div id="excerpt">
<h1 class="title{% if collection.description.size > 0 %}
    {% else %} nodesc{% endif %}">{{ collection.title | escape }}</h1>
    {% if collection.description.size > 0 %}
{{ collection.description }}{% endif %}
</div>
<!-- END EXCERPT -->

window.location.pathname方法就是您想要的。

对于您提到的示例页面,它返回以下字符串:

/collections/women/jewelry

从这里,您可以运行search来查找字符串是否包含jewelry:

var thisString = window.location.pathname;
function doSearch(regex, string) {
  var findMatch = string.search(regex);
  if (findMatch !== -1) return true;
  else return false;
}
if (doSearch(/jewelry/, thisString) === true) {
  // create breadcrumb navigation
}​