2013-09-03 3 views
0

내 PhoneGap android 앱에서 내 Index.html 페이지의 OnDeviceReady() 메서드에 backbutton 이벤트 리스너에 대해 다음 함수를 추가했습니다. 직면BackButton 이벤트가 PhoneGap의 다른 페이지에 영향을 미침

document.addEventListener("backbutton", function(e) { 
      var sPath=window.location.pathname; 
      var sPage = sPath.substring(sPath.lastIndexOf('/') + 1); 
      if(sPage == "index.html"){ 
       e.preventDefault(); 
       // My action 
       return false; 

      } else { 
       return true; 
      } 
    }, false); 

문제 : 내 홈페이지 (Index.html 것)의 작업 벌금
. 다시 버튼을 누르면 닫힙니다.
그러나 다른 모든 페이지 (Page1.html, Page2.html, Page3.html, Page4.html) 나는 backbutton 이벤트 수신기를 만들지 않았지만 아무 키도 누르지 않으면 아무 일도 발생하지 않습니다. 다음과 같은 자바 스크립트 코드를 추가하여 시도 할 수 있습니다

+0

는 어떻게 다른 페이지로 홈페이지에서 이동? –

+0

"window.location ="page1.html "; 코드를 사용하여 버튼 클릭 이벤트에서" – Vignesh

답변

1

: 나는 인덱스보다 다른 페이지에 다음 코드를 사용했다

확실히 작동하지만 두 개의 자바 스크립트 파일을 사용

document.addEventListener("backbutton", function(e) { 
      var sPath=window.location.pathname; 
      var sPage = sPath.substring(sPath.lastIndexOf('/') + 1); 
      if(sPage == "index.html"){ 
       e.preventDefault(); 
// Method to close Phonegap application 
       navigator.app.exitApp(); 


       } else { 
    // Method to go back to previous page 
        navigator.app.backHistory(); 
       } 
     }, false); 

다른 방법 .... html :

// Wait for Cordova to load 

    document.addEventListener("deviceready", onDeviceReady, false); 
     // Cordova is ready 

    function onDeviceReady() { 
    document.addEventListener("backbutton", function(e){ 
    e.preventDefault(); 
    navigator.app.backHistory(); 
    return false; 
}, true); 

     } 

홈 페이지에 navigator.app.exitApp(); navigator.app.backHistory()를 제거하십시오.

는 이미 여기에 대답 PhoneGap - android exit on backbutton

+0

sjmach, 도움 주셔서 감사합니다. 두 번째 코드는 잘 작동하지만 단점은 10 페이지에 10 개의 html 파일이있는 경우 모든 10 페이지의 수신기를 구현해야한다는 것입니다. 홈 페이지에 구현하는 동안 뒤로 버튼 이벤트가 다른 페이지에서 작동하지 않는 이유가 궁금합니다 !!!!!! – Vignesh

관련 문제