1

次のコードは、ページのコンテンツを動的に変更しながら、状態オブジェクトに新しい URL をプッシュし続けます。ただし、Backボタンを押し始めて元のページに戻ると、元のコンテンツは表示されず、代わりに次のページのコンテンツが保持されます。どうすれば達成できますか?

<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
a = 0;
  $("p").click(function(){
var stateObj = { note : ++a };
history.pushState(stateObj, "page 2", "http://localhost/foo/"+a);
$(this).text(a);    
  });
  window.addEventListener('popstate', function(e){
  if (history.state){
    $("p").text(e.state.note);
if (location.href == 'http://localhost/hist.php') { $('p').text('If you click on me, I will disappear.'); }
   }
 }, false);
$("div").click(function() { alert(e.state.note); });
  });
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<div>Hi</div>
</body>
</html>
4

1 に答える 1