【问题标题】:BigCommerce: stencil template - way to access query string from URL in template file? [duplicate]BigCommerce:模板模板 - 从模板文件中的 URL 访问查询字符串的方法? [复制]
【发布时间】:2026-02-07 00:55:01
【问题描述】:

BigCommerce 新手,但已在 Magento / Shopify 等中广泛使用模板

使用基于模板的主题

需要一种从 URL 访问查询字符串的方法 - 我之前使用的大多数模板语言都内置了此功能,但对于 Big Commerce,这似乎不是那么简单

(作为背景,我们正在使用不同的 utm_ 变量运行广告系列,并且我们希望根据它们进入的广告系列有条件地显示内容)

有没有人做过这方面的经验?

【问题讨论】:

  • 在浏览器的 JavaScript 中还是在 Stencil 中?如果是模版,则见*.com/q/65968203/1402988
  • 对不起 - 应该澄清这一点。是的,特别是在模板中 - 我知道我可以在 JS 中抓取/解析 URL,但需要能够通过不同的模板块来保持页面轻量级。该解决方案看起来可以解决问题 - 谢谢!

标签: templates query-string bigcommerce


【解决方案1】:

您可以使用常规 javascript 来获取 URL 查询字符串参数。

// You can get the querystring from window.location
alert(`
The current URL is ${window.location}

The querystring is ${window.location.search || '(none)'}`);

有一个专门的方法可以访问查询字符串: https://developers.google.com/web/updates/2016/01/urlsearchparams?hl=en

目前,根据文档,IEll 不支持: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams#browser_compatibility

IE11 的解决方案是使用 polyfill。这是推荐的: https://github.com/ungap/url-search-params

【讨论】:

  • 抱歉,我的错是不清楚 - 寻找一种使用模板本身执行此操作的方法,以便我可以有条件地加载不同的内容块(比使用 JS 显示/隐藏页面加载时间更有效) ) - 感谢您的意见,谢谢!