LoginSignup
2
6

More than 5 years have passed since last update.

GTM:URLの一部を変数に渡す

Last updated at Posted at 2016-08-11

カスタムjavascriptでURLの一部を変数に渡す

www.test.com/list/id123456.html
から123456を抜き出したい

1.パスの前後をカットする

<書き方>
function() {
return location.pathname.slice(8,-5);
}

<解説>
/list/id123456.html の
頭から8文字、つまり"d"のあとから、
おしりから5文字、つまり"."の前までの、
間の文字列を返す

2.特定の文字列と文字列の間を抜き出す

<書き方>
function(){
var href = window.location.href ;
var a = href.split("id");
var b = a[1].split(".html");
return b[0];
}

<解説>
www.test.com/list/id123456.html
を"id"で分割し、"a"に入れる
⇒a[0] = "www.test.com/list/"
 a[1] = "123456.html"

a[1]を".html"で分割し、"b"に入れる
⇒b[0] = "123456"
 b[1] = ""

b[0]を戻す

2
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6