0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScriptでURL取得する

Last updated at Posted at 2021-12-02

split() メソッドを使用して取得してみる

例えば現在のページが
https://??.com/xx/yy/zz だったとする
このURLのyyだけを取得したい

方法としては,
locationオブジェクトのhrefプロパティで,現在のページURLを取得し,
split()メソッドで指定した文字列で分割
というやり方です.

var url_split = location.href.split('/');  // /(スラッシュ)で取得したURLを分割し配列化
var url_split_yy = url_split[4];

console.log(url_split_yy);
//出力"yy"
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?