2

私はこれに似たURLを持っています:

www.mysite.com/products/

私はこれを使用してパス名に対してテストしていました:

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

しかし、私が遭遇した問題は、上記がサブフォルダーに対しても実行されることでした。これは私が望まないことです。

www.mysite.com/products/sub-folder/

window.location.pathnameは、上記のjQueryよりも役立つと思います。しかし、その中のサブディレクトリではなく、トップレベルのディレクトリのみをターゲットにする方法がわかりませんか?

4

2 に答える 2

2

正規表現の最後にを追加し$ます。

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

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

于 2011-06-17T22:35:19.460 に答える
0
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;
于 2011-06-17T22:39:29.663 に答える