Javascript では、window.location を使用してリクエストされた URL を取得することができます。ここでは、その取得結果例を掲載しています。
スポンサーリンク
window.location プロパティ
| プロパティ | 説明 |
|---|---|
| hash | # 記号に続くURL の部分 |
| host | ホスト名とポート番号。 |
| hostname | ホスト名(ポート番号を含まない) |
| href | 完全な URL |
| pathname | パス(ホストからの相対) |
| port | URL のポート番号。 |
| protocol | URL のプロトコル |
| search | ? 記号に続く URL の部分。? 記号も含みます |
では、実際に以下の URL でアクセスされた場合の取得結果を確認してみます。
URL : http://localhost:8080/test/test.html?p1=1&p2=2#hash
alert('location.href = ' + window.location.href);
alert('location.protocol = ' + window.location.protocol);
alert('location.host = ' + window.location.host);
alert('location.hostname = ' + window.location.hostname);
alert('location.port = ' + window.location.port);
alert('location.pathname = ' + window.location.pathname);
alert('location.search = ' + window.location.search);
alert('location.hash = ' + window.location.hash);
// 出力結果
// location.href = http://localhost:8080/test/test.html?p1=1&p2=2#hash
// location.protocol = http:
// location.host = localhost:8080
// location.hostname = localhost
// location.port = 8080
// location.pathname = /test/test.html
// location.search = ?p1=1&p2=2
// location.hash = #hash
詳細は、以下のサイトを参照ください。英語版のほうが情報が豊富です。
参考