달력

52024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

페이지 뒤로 가기 방지. 

 

가끔은 (정말 가끔이다 믿어도 좋다.) 

 

특정 페이지에서 뒤로가기를 제한해야 하는 경우가 발생하는데! (인증이라던가, 인증이라던가, 인증이라던가)

 

그럴때 스크립트 단에서 해당 이벤트 자체를 취소하는 방법이다.

 

history api를 사용한 부분이 html5에서 동작하는 부분이며,

아래 else에 처리된 부분이 미지원 브라우저에서 동작하는 부분이다.

 


if (history.pushState !== undefined)
{
    history.pushState(null, null, location.href);
    window.onpopstate = function (event)
    {
        history.go(1);
    };
}
else
{
    var storedHash = window.location.hash;
    function changeHashOnLoad()
    {
        window.location.href += "#";
        setTimeout("changeHashAgain()", "50");
    }
 
    function changeHashAgain()
    {
        window.location.href += "1";
    }
 
    function restoreHash()
    {
        if (window.location.hash != storedHash)
        {
            window.location.hash = storedHash;
        }
    }
 
    if (window.addEventListener)
    {
        window.addEventListener("hashchange", function ()
        {
            restoreHash();
        }, false);
    }
    else if (window.attachEvent)
    {
        window.attachEvent("onhashchange", function ()
        {
            restoreHash();
        });
    }
    $(window).load(function () { changeHashOnLoad(); });
}


Posted by 은하비류연
|