2

我有一个与此类似的网址:

www.mysite.com/products/

我用它来测试路径名:

if (/\/products\//.test(window.location)) {
_gaq.push(['_trackPageview', '/products/landing']);
}

但是我遇到的问题是上面的内容也会对子文件夹执行,我不希望这样:

www.mysite.com/products/sub-folder/

我在想 window.location.pathname 将比上面的 jQuery 帮助我更多。但我不确定如何只定位顶级目录,而不是其中的子目录?

4

2 回答 2

2

$在你的正则表达式末尾添加一个:

if (/\/products\/$/.test(window.location)) {
_gaq.push(['_trackPageview', '/products/landing']);
}

示例:http: //jsfiddle.net/niklasvh/feK4A/

于 2011-06-17T22:35:19.460 回答
0
window.location.pathname.indexOf("/",1);

所以现在你可以做

var indOf = window.location.pathname.indexOf("/",1);
var myStr = window.location.pathname.substr(0,indOf+1 );

alert( myStr );  // gives you what you want;
于 2011-06-17T22:39:29.663 回答