Javascriptで現在のURL取得

今回は、Javascriptで現在のURLを取得する方法についてみていきたいと思います。

location

ブラウザで表示したWebページのアドレス(URL)に関する情報が格納されています。

サンプル

現在のURL:「http://www.anveloper.com:8080/index.html?test=2」

全体URL取得

location.href
⇨http://www.anveloper.com:8080/index.html?test=2

ドメイン名を取得する

location.host
⇨www.anveloper.com:8080

ポートを取得する

location.port
⇨8080

プロトコルを取得する

location.protocol
⇨http

パスを取得する

location.pathname
⇨/index.html

パラメータを取得する

location.search/
⇨?test=2

参考

https://developer.mozilla.org/ja/docs/Web/API/Location

コメントを残す