locationオブジェクトのプロパティは、以下のようなものがあります。
location.host ホスト情報を取得
location.hostname ホスト名を取得
location.href 現在のURLを取得 書き換えるとそのURLに移動
location.search 現在のURLの?以降の情報を取得
documentオブジェクトのときと同じように、6-2-1で作成したrenshu6-2-1に書き加えて実行させてみましょう
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Javascriptの練習</title>
<script>
document.write("現在のタイトルは"+document.title+"です<br>");
document.write("ファイルの最終更新日は"+document.lastModified+"です<br>");
document.write("現在のURLは"+document.URL+"です<br>");
document.write("ドキュメントのドメインは"+document.domain+"です<br>");
document.write("現在のクッキーは"+document.cookie+"です<br>");
// ここから追加になります。
document.write("ホスト情報は"+location.host+"です<br>");
document.write("ホスト名は"+location.hostname+"です<br>");
document.write("現在のURLは"+location.href+"です<br>");
document.write("?以降の情報は"+location.search+"です<br>");
</script>
</head>
<body>
<h1>Javascriptの練習です</h1>
</body>
</html>
今回は、renshu6-2-1.html?test=test みたいな形で、情報を付加した形で実行してみます。
ローカルの情報だと、ホスト情報などは表示されませんが、ネット上にアップした場合は、以下のような表示になります。