URL.href - 完全なアドレス
hrefは、URLのプロパティです。オブジェクトの完全なアドレスを返します。
概要
- 名前
- href
- 所属
- URL
- IDL
stringifier attribute USVString href;
- 仕様書
- https://url.spec.whatwg.org/#dom-url-href
説明
オブジェクトの完全なアドレスを返す。代入して値を更新できる。
チュートリアル
このプロパティにはstringifier属性があります。URLを文字列として取り扱う時、URL.hrefと同じ値になります。下記は同じ出力となります。
element.textContent = URL.href ;
element.textContent = URL ;
値を代入する時も同様です。
URL.href = "https://syncer.jp/" ;
URL = "https://syncer.jp/" ;
デモ
URL.hrefのデモです。
<!-- このコードは編集できます。 -->
<!DOCTYPE html>
<html>
<style>
body { word-break: break-all ; tab-size: 2 ; }
div#result { white-space: pre-wrap ; }
</style>
<body>
<div id="result"></div>
<script>
var url = new URL( "https://hoge:fuga@syncer.jp:80/page.html?piyo=1&toto=2#foo" ) ;
// 代入
// url.href = "https://example.com/" ;
// 取得
console.log( url ) ;
appendText( url + "\n\n" ) ;
appendText( "href: " + url.href + "\n" ) ;
appendText( "origin: " + url.origin + "\n" ) ;
appendText( "protocol: " + url.protocol + "\n" ) ;
appendText( "username: " + url.username + "\n" ) ;
appendText( "password: " + url.password + "\n" ) ;
appendText( "host: " + url.host + "\n" ) ;
appendText( "hostname: " + url.hostname + "\n" ) ;
appendText( "port: " + url.port + "\n" ) ;
appendText( "pathname: " + url.pathname + "\n" ) ;
appendText( "search: " + url.search + "\n" ) ;
appendText( "hash: " + url.hash + "\n" ) ;
appendText( "toJSON(): " + "\n" + JSON.stringify( url.toJSON() ) + "\n" ) ;
function appendText ( text ) {
document.getElementById( "result" ).appendChild( document.createTextNode( text ) ) ;
}
</script>
</body>
</html>
サポート状況
Chrome | Firefox | Safari | Edge | IE | Opera | iOS Safari | Android |
---|---|---|---|---|---|---|---|
● 25+ | ● 26+ | ● 7.1+ | ● | × | ● 19+ | ● 8.3+ | ● 4.4+ |
関連記事
- URL
- URLは、URLアドレスの情報を管理するための機能を備えたインターフェイスです。
- Document.open()
- open()は、Documentのメソッドです。ドキュメントを開き、関連メソッドで書き込みができる状態にします。このメソッドは3つの引数を指定すると、全く別の性質を持ち、Window.open()と同じ働きをします。
- DocumentType.name
- nameは、DocumentTypeのプロパティです。文書型宣言の名前を返します。
- URL.port
- portは、URLのプロパティです。オブジェクトのポート番号を返します。