1
다음 코드는 새로운 URL을 상태 객체에 푸시하면서 페이지의 내용도 동적으로 변경합니다. 그러나 버튼을 눌러 원래 페이지로 돌아 가면 원본 콘텐츠가 표시되지 않고 대신 다음 페이지의 콘텐츠가 유지됩니다. 그것을 어떻게 성취합니까?window.popstate 이후 원본 콘텐츠를 복원하는 방법은 무엇입니까?
<!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>