[Phonegap 3.4] phonegap.js功能不在index.html以外的其他页面上工作

时间:2014-04-27 09:42:39

标签: javascript android html cordova

我正在研究我的Android手机PhoneGap应用程序(我使用的是Android 2.3模拟器),并且我解决了一些问题。

我的项目有两页:index.html& page.html中。

我使用

从index.html导航到page.html
self.location="page.html"+'?'+textAImp+"&12";

此代码位于我的“js / index.js”[创建项目时创建的默认文件]。

它工作正常,它让我到我的“page.html”,但当我尝试使用“js / index.js”中包含的相同功能时(我在2页中使用相同的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”

中工作正常

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

感谢您的帮助:)。

1 个答案:

答案 0 :(得分: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>