DOM

History.state プロパティ

 編集:2022.10.26 by T.A.

[Edge]。読取専用?。→

履歴スタックの一番上の状態を表す値を返します。

これは popstate イベントを待つことなく、状態を見ることができる方法です。

構文
History.state
History History オブジェクト。

履歴スタックの一番上の状態です。この値は pushState() または replaceState() メソッドが使用されていない限り null です。

mdnより

以下のコードでは、history.state の値を pushState() メソッドを使用して値を履歴にプッシュする前に、ログに記録しています。次の行では、再びコンソールに値が記録され、history.state に値があることが分かります。

// まだ履歴スタックを変更していないため、 null になります
console.log(`History.state before pushState: ${history.state}`);

// スタックに何かプッシュします
history.pushState({name: 'Example'}, "pushState example", 'page3.html');

// 状態は値になりました。
console.log('History.state after pushState: ', history.state);

HTML:

<A href="../Com/Test.htm">リンク</A>

JavaScript:

alert(history.state);
history.pushState({name: 'Example'}, "pushState example", "../Com/Test.htm");  // スタックに何かプッシュする。
	//エラー発生:Failed to execute 'pushState' on 'History': A history state object with URL 'file:///C:/Users/asmin/Documents/HTML%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9forEdge/HTML-Rifarennsu/DOM/Com/Test.htm' cannot be created in a document with origin 'null' and URL 'file:///C:/Users/asmin/Documents/HTML%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9forEdge/HTML-Rifarennsu/DOM/History/state.htm'
	//'History' で 'pushState' を実行できませんでした。…
D=history.state;  //

pushState()


テスト