定位 window.location.pathname
我有一个与此类似的网址:
www.mysite.com/products/
我用它来测试路径名:
if (/\/products\//.test(window.location)) {
_gaq.push(['_trackPageview', '/products/landing']);
}
但我遇到的问题是上面的内容也会对子文件夹执行,这是我不想要的:
www.mysite.com/products/sub-folder/
我正在考虑 window.location .pathname 比上面的 jQuery 更能帮助我。但我不确定如何仅定位顶级目录,而不定位其中的子目录?
I have a url similiar to this:
www.mysite.com/products/
I was using this to test against the pathname:
if (/\/products\//.test(window.location)) {
_gaq.push(['_trackPageview', '/products/landing']);
}
But the problem I was running into was the above would execute for sub-folders as well, which I do not want:
www.mysite.com/products/sub-folder/
I'm thinking window.location.pathname will help me out more than the above jQuery. But I'm unsure how to target only the top-level directory, and not the sub directories within it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在正则表达式末尾添加
$
:示例:http://jsfiddle.net /niklasvh/feK4A/
Add a
$
at the end of your regexp:example: http://jsfiddle.net/niklasvh/feK4A/
所以现在你可以做
so now you can do