1

我正在尝试获取此 URL“http://www.example.com/womens-collection/ceramic ”以重定向到 URL“http://www.example.com/the-ceramic”。下面是代码

   if(window.location.pathname == "/womens-collection/ceramic") {
   //alert("worked");
   document.location = "/the-ceramic";
   //$('#jbw-spot-mobile_details').toggleClass('opened')}

工作正常,但我不希望任何其他产品 URL 受此影响,例如“http://www.example.com/womens-collection /ceramic?page=shop.product_details

我怎样才能让 jquery 只识别“/womens-collection/ceramic”而不是之后的任何东西?

4

2 回答 2

2
regex = /(/womens-collection/ceramic)$/i; // Using the i flag for case insensitivity. Feel free to remove if you want.
path = window.location.pathname;
if (path.match(regex)) {
   // Do stuff.
}

只需使用正则表达式匹配路径的末尾

于 2012-08-30T19:21:29.003 回答
1

window.location.search返回 QS 部分。

if(window.location.pathname == "/womens-collection/ceramic")
{
    if(!window.location.search)
    {
        // do your thing
    }
}
于 2012-08-30T19:17:30.567 回答