[英]concat or append a fixed query string to a url in javascript
我想将?nowprocket查询字符串添加到包含“ /product/ ”的网址
示例: https : //tipmrebuilders.com/product/rebuilt-oem-tipm-for-2011-jeep-liberty-with-towing-package-part-04692330/?nowprocket
我从这行代码开始,但我无法继续,因为我不知道下一步该做什么。
if (window.location.href.indexOf("/product/") > -1) {
\\?
}
请指教。
我会使用URL API ,它有方法来检查查询参数是否存在,如果不存在则添加它
// DEMO ONLY - not for production initDemoUrl() const url = new URL(location.href); if( url.pathname.includes('/product/') && !url.searchParams.has('nowprocket') ){ url.searchParams.append('nowprocket',''); // uncomment following to reload page // location.href = url } console.log(url) // DEMO ONLY function initDemoUrl(){ history.pushState(null,null, '/product/some-slug') }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.