window.historyとwindow.locationについての個人的メモ

現在表示しているページのURLはwindowオブジェクトのlocationプロパティに保存されている。
例えば、http://lochttp://wbs-newworld.appspot.com/hashStudy.htmlにアクセスした場合、window.locationの値は「http://wbs-newworld.appspot.com/hashStudy.html」となる。

window.locationに保持された値はwindow.historyに順次保存される。

さて、window.locationオブジェクトにはhashプロパティというものがあり、hashプロパティを変更すると、window.locationがwindow.historyに新たに追加される。
例えば、下記HTMLがあったとして、そのページのURLはhttp://lochttp://wbs-newworld.appspot.com/hashStudy.htmlとしよう。

<a href="#1" name="next1">次点1</a><br>
<a href="#2" name="next2">次点2</a><br>
<a href="#3" name="next3">次点3</a><br>

次点1、2、3の順にリンクを押すと、window.locationは、以下の順で変わる。

  1. http://wbs-newworld.appspot.com/hashStudy.html
  2. http://wbs-newworld.appspot.com/hashStudy.html#1
  3. http://wbs-newworld.appspot.com/hashStudy.html#2
  4. http://wbs-newworld.appspot.com/hashStudy.html#3

その後、window.history.back()を3回実行すると、次点3のリンクに戻り、次点2のリンクに戻り、次点1のリンクに戻る。