我有一个类似于这个的url:
www.mysite.com/products/我使用下面的代码来测试路径名:
if (/\/products\//.test(window.location)) {
_gaq.push(['_trackPageview', '/products/landing']);
}但我遇到的问题是,上面的代码也会对子文件夹执行,这是我不想要的:
www.mysite.com/products/sub-folder/比起上面的jQuery,我想window.location.pathname会帮我更多。但我不确定如何只针对顶层目录,而不是其中的子目录?
发布于 2011-06-18 06:35:19
在您的regexp末尾添加一个$:
if (/\/products\/$/.test(window.location)) {
_gaq.push(['_trackPageview', '/products/landing']);
}示例:http://jsfiddle.net/niklasvh/feK4A/
发布于 2011-06-18 06:39:30
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;https://stackoverflow.com/questions/6392486
复制相似问题