SYNCERのロゴ
アイキャッチ画像

Location.href - 完全なアドレス

hrefは、Locationのプロパティです。ロケーションの完全なアドレスを返します。プロパティに値を代入して、新しいロケーションに遷移させることもできます。

概要

名前
href
所属
Location
IDL
[Unforgeable] stringifier attribute USVString href;
仕様書
https://html.spec.whatwg.org/multipage/history.html#dom-location-href

説明

ロケーションの完全なアドレスを返す。代入すると、そのアドレスに遷移する。

チュートリアル

このプロパティにはstringifier属性があります。Locationを文字列として取り扱う時、Location.hrefと同じ値になります。下記は同じ出力となります。

element.textContent = location.href ;
element.textContent = location ;

値を代入する時も同様です。

location.href = "https://syncer.jp/" ;	// "https://syncer.jp/"に遷移
location = "https://syncer.jp/" ;	// "https://syncer.jp/"に遷移

デモ

Location.hrefのデモです。

<!-- このコードは編集できます。 -->

<!DOCTYPE html>
<html>
<body>
<script>
var value = location.href ;

// 取得
console.log( value ) ;
document.body.appendChild( new Text( value ) ) ;

// 代入
// location.href = "https://example.com/" ;
</script>
</body>
</html>

サポート状況

ChromeFirefoxSafariEdgeIEOperaiOS SafariAndroid
  • Twitterでシェア
  • Facebookでシェア
  • Google+でシェア
  • はてなブックマークでシェア
  • pocketに保存
  • LINEでシェア
更新履歴
2017年10月8日 (日)
コンテンツを公開しました。