2010-05-17 7 views
0

SharePoint의 응용 프로그램 페이지 중 하나에 사용자 지정 웹 파트가 있습니다. 이 페이지는 이미 Windows beforeunload 자바 스크립트 이벤트에서 실행되는 함수가있는 것 같습니다.aspx 페이지에서 기본 beforeunload 이벤트를 연결하는 방법?

제 문제는 Windows beforeunload 이벤트에서 일부 클라이언트 측 코드 (웹 파트의 저장되지 않은 변경 사항에 대해 사용자에게 프롬프트)를 실행해야한다는 것입니다.

어떻게하면됩니까? 내 기능을 호출 할뿐만 아니라 기본 이벤트를 시작하도록 내버려 두는 건가요?

감사합니다.

Nikhil.

답변

2

onbeforeunload 이벤트에 할당 된 기존 처리기를 확인하고 해당 핸들러가있는 경우 처리기로 바꾸기 전에 참조를 저장해야합니다.

다음 스크립트 출력을 방출 할 수 귀하의 웹 파트는이 작업을 수행합니다 : 이것은 당신이 페이지에 당신의 자신의 논리를 주입 어떤 코드 때 페이지 언로드를 실행로서 자신의 결정을 할 수 있도록해야

<script type="text/javascript"> 

// Check for existing handler 
var fnOldBeforeUnload = null; 
if (typeof(window.onbeforeunload) == "function") { 
    fnOldBeforeUnload = window.onbeforeunload; 
} 

// Wire up new handler 
window.onbeforeunload = myNewBeforeUnload; 

// Handler 
function myNewBeforeUnload() { 
    // Perform some test to determine if you need to prompt user 
    if (unsavedChanges == true) { 
     if (window.confirm("You have unsaved changes. Click 'OK' to stay on this page.") == true) { 
      return false; 
     } 
    } 

    // Call the original onbeforeunload handler 
    if (fnOldBeforeUnload != null) { 
     fnOldBeforeUnload(); 
    } 
} 

</script> 

.

관련 문제