Javascript [Phonegap 3.4]Phonegap.js函数在index.html以外的其他页面上不起作用

Javascript [Phonegap 3.4]Phonegap.js函数在index.html以外的其他页面上不起作用,javascript,android,html,cordova,Javascript,Android,Html,Cordova,我正在为Android开发我的First PhoneGap应用程序,我使用的是Android 2.3模拟器,我遇到了一些问题 我的项目有两个页面:index.html和page.html 我使用 self.location="page.html"+'?'+textAImp+"&12"; 此代码位于my js/index.js[创建项目时创建的默认文件]中 它工作得很好,可以让我访问page.html,但是当我尝试使用js/index.js中包含的相同函数时,我在两个页面中使用了相同的j

我正在为Android开发我的First PhoneGap应用程序,我使用的是Android 2.3模拟器,我遇到了一些问题

我的项目有两个页面:index.html和page.html

我使用

self.location="page.html"+'?'+textAImp+"&12";
此代码位于my js/index.js[创建项目时创建的默认文件]中

它工作得很好,可以让我访问page.html,但是当我尝试使用js/index.js中包含的相同函数时,我在两个页面中使用了相同的js函数,只是在我的第二个页面上不起作用

adb 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中工作得很好

<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>
我将我的页面代码和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');
                        }
                };

谢谢您的帮助:

我不确定,但您可以尝试代替window.onload


我不确定,但您可以尝试代替window.onload


您是否尝试过在page.html中使用onDeviceReady替代window.onload?您是否尝试过在page.html中使用onDeviceReady替代window.onload?这里是init的}括号,其中是init的}括号