Home » ★JavaScriptリファレンス

★JavaScriptリファレンス

location.search …… 現在ページURLのサーチ情報を参照する
Internet Explorer
Google Chrome
Safari
Firefox
Opera

locationオブジェクトのsearchプロパティは、現在ページURLのサーチ情報(?で始まる検索クエリ部分)を参照します。

仮に現在URLが以下の場合、locationオブジェクトの各プロパティの値は以下の通りです。

現在URLの例:http://www.htmq.com:8000/js/index.php?q=javascript#link
↓↓↓
location.hrefは「http://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.search
戻り値
現在ページ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>
↓↓↓

ブラウザ上の表示

location.hrefは「https://htmqcom1.wpengine.com/tech/js/location_search」
location.protocolは「https:」
location.hostnameは「www.htmq.com」
location.hostは「www.htmq.com」
location.portは「」
location.pathnameは「/js/location_search」
location.searchは「?aaa」
location.hashは「」

■関連項目

location.href …… 現在ページURLを参照する
location.protocol …… 現在ページURLのプロトコルを参照する
location.hostname …… 現在ページURLのホスト名を参照する
location.host …… 現在ページURLのホスト情報を参照する
location.port …… 現在ページURLのポート番号を参照する
location.pathname …… 現在ページURLのパス名を参照する
location.search …… 現在ページURLのサーチ情報を参照する
location.hash …… 現在ページURLのハッシュ部分を参照する

location.reload() …… ページをリロード(再読み込み)する
location.replace() …… 指定したページへ移動する

Lorem Ipsum is simply dummy text

    By signing up, you agree to our Terms and Privacy Policy. Unsubscribe anytime.