2014-04-27 33 views
1

我正在研究Android的PhoneGap應用程序(我使用的是Android 2.3模擬器),並且遇到了一些問題。[Phonegap 3.4] phonegap.js函數不能在除index.html以外的其他頁面上工作

我在我的項目上有2個頁面:index.html & page.html。

我從index.html的導航使用

self.location="page.html"+'?'+textAImp+"&12"; 

此代碼位於我「的js/index.js」到page.html中[當你創建項目的默認文件創建。

它工作正常,它讓我到我的「page.html」,但是當我嘗試使用「js/index.js」中包含的相同函數(我在兩頁中使用相同的js函數)時,它只是不工作在我的第二頁上!

亞行logcat給了我這樣的:

D/CordovaLog( 598): file:///android_asset/www/phonegap.js: Line 1548 : Could not find cordova.js script tag. Plugin loading may fail. 
I/Web Console( 598): Could not find cordova.js script tag. Plugin loading may fail. at file:///android_asset/www/phonegap.js:1548 
D/CordovaLog( 598): file:///android_asset/www/js/index.js: Line 128 : ReferenceError: Can't find variable: Connection 
E/Web Console( 598): ReferenceError: Can't find variable: Connection at file:///android_asset/www/js/index.js:128 
D/CordovaWebViewClient( 598): onPageFinished(file:///android_asset/www/page.html?20.09724882&12) 
D/CordovaActivity( 598): onMessage(onPageFinished,file:///android_asset/www/page.html?20.09724882&12) 

我感到困惑,因爲它在我「的index.html」

罰款我把我的網頁代碼和phonegap.js碼如下:

「的index.html」

<html> 
    <head> 
     <meta charset="utf-8" /> 
     <meta name="format-detection" content="telephone=no" /> 
     <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> 
     <link rel="stylesheet" type="text/css" href="css/index.css" /> 
     <title>Hello World</title> 
    </head> 
    <body> 
     <div class="app"> 
      <h1>PhoneGap</h1> 
      <div id="deviceready" class="blink"> 
       <p class="event listening">Connecting to Device</p> 
       <a href="#" onclick="jsfunction();"><p id="parag" class="event received">Device is Ready</p></a> 

      </div> 
     </div> 
     <script type="text/javascript" src="phonegap.js"></script> 
     <script type="text/javascript" src="js/index.js"></script> 
     <script type="text/javascript"> 
      app.initialize(); 

     </script> 

    </body> 
</html> 

「page.html中」

<html> 
    <head> 
     <meta charset="utf-8" /> 
     <meta name="format-detection" content="telephone=no" /> 
     <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> 
     <link rel="stylesheet" type="text/css" href="css/index.css" /> 
     <title>Hello World</title> 
    </head> 
    <body> 
     <p id="parag">Hamza !</p> 

     <script type="text/javascript" src="phonegap.js"></script> 
     <script type="text/javascript" src="js/index.js"></script> 
     <script type="text/javascript"> 
        window.onload = function() 
        { 
          var a = self.location.href.split('?')[1].split('&'); 
          document.getElementById("parag").innerHTML=a[0] + "<br>" + a[1]; 
          alert('Query string: '+self.location.search); 
          jsfunction() ; 
        }; 
      </script> 

    </body> 
</html> 

「的js/index.js」

var app = { 
    // Application Constructor 
    initialize: function() { 
     this.bindEvents(); 
    }, 
    // Bind Event Listeners 
    // 
    // Bind any events that are required on startup. Common events are: 
    // 'load', 'deviceready', 'offline', and 'online'. 
    bindEvents: function() { 
     document.addEventListener('deviceready', this.onDeviceReady, false); 
    }, 
    // deviceready Event Handler 
    // 
    // The scope of 'this' is the event. In order to call the 'receivedEvent' 
    // function, we must explicity call 'app.receivedEvent(...);' 
    onDeviceReady: function() { 
     app.receivedEvent('deviceready'); 
    }, 
    // Update DOM on a Received Event 
    receivedEvent: function(id) { 
     var parentElement = document.getElementById(id); 
     var listeningElement = parentElement.querySelector('.listening'); 
     var receivedElement = parentElement.querySelector('.received'); 

     listeningElement.setAttribute('style', 'display:none;'); 
     receivedElement.setAttribute('style', 'display:block;'); 

     console.log('Received Event: ' + id); 
    } 
}; 

function jsfunction() 
       { 
         checkConnection(); 
         window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, reqFSsuccess, reqFSerror); 
       }; 

       function reqFSsuccess(fileSystem) 
       { 
         fileSystem.root.getFile("test.txt", {create: true, exclusive: false}, createFileSuccess, createFileFail); 
       }; 

          function createFileSuccess(fileEntry) 
          { 

            var fileTransfer = new FileTransfer(); 
           var uri = encodeURI("http://someURLThatWork"); 
           fileTransfer.download 
           (
             uri, 
             fileEntry.toURL(), 
             function(theFile) 
             { 
               console.log("download complete: " + theFile.toURI()); 
                theFile.file(readFile, readFileFail); 
             }, 
             function(error) 
             { 
               console.log("download error source " + error.source); 
                console.log("download error target " + error.target); 
                console.log("upload error code: " + error.code); 
             } 
           ); 
          }; 

          function createFileFail(error) 
          { 
            alert("Failed to create file: " + error.code); 
          }; 

       function reqFSerror(error) 
       { 
         alert("FileSystem not loaded !") 
       }; 

       function readFile(file) 
       { 
         var reader = new FileReader(); 
         reader.onloadend = function(evt) 
         { 
           console.log("read success"); 
           console.log(evt.target.result); 
           alert(evt.target.result); 
           var aIprim, textAImp = 0; 
           var i = 0; 
           var mySplittedText = evt.target.result.split("\n"); 
           do { 
             textAImp = textAImp + 2 * mySplittedText[i]; 
             i++; 
           } while (i < mySplittedText.length); 
           document.getElementById("parag").innerHTML=textAImp+"&12"; 
           self.location="page.html"+'?'+textAImp+"&12"; 
           //evt.target.result.replace(/\n/g, '<br>'); 

         }; 
         reader.readAsText(file); 
       }; 

       function readFileFail(error) 
       { 
         alert("File not read !") 
       }; 


       function checkConnection() 
       { 
        var networkState = navigator.connection.type; 

         if (networkState == Connection.NONE) 
         { 
            alert('No network connection'); 
         } 
         else 
         { 
            alert('Connection'); 
         } 
       }; 

並感謝您的幫助:)

+0

您是否曾嘗試在page.html中使用onDeviceReady inststead window.onload – hkazemi

回答

4

我不知道,但你可以嘗試,而不是window.onload

<script type="text/javascript"> 
      $(document).ready(init); 

    function init() { 
       document.addEventListener("deviceready", deviceInfo, true);  
        } 



function onDeviceReady() { 
      var a = self.location.href.split('?')[1].split('&'); 
      document.getElementById("parag").innerHTML=a[0]<br>" + a[1]; 
      alert('Query string: '+self.location.search); 
      jsfunction() ; 
    }           

          </script> 
+0

「init()'' – Shell

+1

忘記那一個@Nimesh –

+0

順便說一句建議是好的。 – Shell

相關問題