开发者

Targeting window.location.pathname

开发者 https://www.devze.com 2023-03-13 15:55 出处:网络
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)) {

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开发者_StackOverflow 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?


Add a $ at the end of your regexp:

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

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


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号