Skip to content Skip to sidebar Skip to footer

Targeting Window.location.pathname

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', '/p

Solution 1:

Add a $ at the end of your regexp:

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

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

Solution 2:

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;

Post a Comment for "Targeting Window.location.pathname"