定位 window.location.pathname

发布于 2024-11-16 04:06:12 字数 400 浏览 7 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欲拥i 2024-11-23 04:06:12

在正则表达式末尾添加 $

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

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

Add a $ at the end of your regexp:

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

example: http://jsfiddle.net/niklasvh/feK4A/

罪歌 2024-11-23 04:06:12
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;
window.location.pathname.indexOf("/",1);

so now you can do

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

alert( myStr );  // gives you what you want;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文