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

Location.assign() - ロケーションを割り当てる

assign()は、Locationのメソッドです。新しいロケーションに遷移させます。hrefに値を代入するのと同じです。

概要

名前
assign
所属
Location
IDL
[Unforgeable] void assign(USVString url);
仕様書
r

説明

引数(url)には、対象のアドレスを指定します。

チュートリアル

Location.assign()は、Location.hrefに値を代入するのと同じです。

// 現在のアドレス
// "https://syncer.jp/1.html"

// メソッドを実行
location.assign( "https://syncer.jp/2.html" ) ;	// "https://syncer.jp/2.html"に遷移

// プロパティに値を代入しても同じ
location.href = "https://syncer.jp/2.html" ;	// "https://syncer.jp/2.html"に遷移

デモ

Location.assign()のデモです。

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

<!DOCTYPE html>
<html>
<body>
<button id="run">実行</button>
<script>
document.getElementById( "run" ).onclick = function () {
	location.assign( "https://example.com/" ) ;
}
</script>
</body>
</html>

サポート状況

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