location.hostname ……
広告
locationオブジェクトのhostnameプロパティは、現在ページURLのホスト名を参照します。
尚、ホスト名とポート番号の両方を同時に取得する場合にはhostプロパティを使用します。
仮に現在URLが以下の場合、locationオブジェクトの各プロパティの値は以下の通りです。
現在URLの例:https://www.htmq.com:8000/js/index.php?q=javascript#link
↓↓↓
location.hrefは「https://www.htmq.com:8000/js/index.php?q=javascript#link」 location.protocolは「http:」 location.hostnameは「www.htmq.com」 location.hostは「www.htmq.com:8000」 location.portは「8000」 location.pathnameは「/js/index.php」 location.searchは「?q=javascript」 location.hashは「#link」
■構文・引数・戻り値
- 構文
- location.hostname
- 戻り値
- 現在ページURLのホスト名
■使用例
HTML + JavaScriptソース
<script> document.write("<div>location.hrefは「" + location.href + "」</div>"); document.write("<div>location.protocolは「" + location.protocol + "」</div>"); document.write("<div>location.hostnameは「" + location.hostname + "」</div>"); document.write("<div>location.hostは「" + location.host + "」</div>"); document.write("<div>location.portは「" + location.port + "」</div>"); document.write("<div>location.pathnameは「" + location.pathname + "」</div>"); document.write("<div>location.searchは「" + location.search + "」</div>"); document.write("<div>location.hashは「" + location.hash + "」</div>"); </script>
↓↓↓
ブラウザ上の表示
“);
document.write(“
location.protocolは「” + h(location.protocol) + “」
“);
document.write(“
location.hostnameは「” + h(location.hostname) + “」
“);
document.write(“
location.hostは「” + h(location.host) + “」
“);
document.write(“
location.portは「” + h(location.port) + “」
“);
document.write(“
location.pathnameは「” + h(location.pathname) + “」
“);
document.write(“
location.searchは「” + h(location.search) + “」
“);
document.write(“
location.hashは「” + h(location.hash) + “」
“);