3
2

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 1 year has passed since last update.

[JavaScript] URLからwindow.location.XXXで色々クエリを取得する。

Last updated at Posted at 2021-10-09

特定のクエリが変わった時だけレンダリングを行うという処理を書こうと思った時に、window.location.XXXのXXXの部分の指定に色々あるということを学んだので、メモ。

##window.location.XXXの色々
URLがhttps://higu.com:8080/higu/scraps?status=openv#hereの場合

window.location.href      // -> "https://higu.com:8080/higu/scraps?status=open#here"
window.location.protocol  // -> "https:"
window.location.host      // -> "higu.com:8080"
window.location.hostname  // -> "higu.com"
window.location.port      // -> "8080"
window.location.pathname  // -> "/higu/scraps"
window.location.search    // -> "?status=open"
window.location.hash      // -> "#here"
window.location.origin    // -> "https://higu.com:8080"

##その他の便利な機能
URLがhttps://higu.com:8080/higu/scraps?status=open#hereの場合

###?の後のクエリだけ欲しい

window.location.search.substring(1)  // -> "status=openv#here"

###特定の場所のクエリが欲しい

ver query = window.location.href;

// /higuの前
query.split("/higu")[0];  // -> "https://higu.com:8080"

// /higuのの後
query.split("/higu")[1];  // -> "/scraps?status=openv#here"

// status=openだけ
var tmp = query.split("/scraps?")[1];
tmp.split("#here")[0];  // -> "status=open"

参考:こちら

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?