簡體   English   中英

[Phonegap 3.4] phonegap.js函數無法在除index.html之外的其他頁面上使用

[英][Phonegap 3.4]phonegap.js functions not working on other page than index.html

我正在開發適用於Android的fisrt PhoneGap應用程序(我使用的是Android 2.3模擬器),但遇到了一些問題。

我的項目有2個頁面:index.html和page.html。

我使用以下方法從index.html導航到page.html

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

這段代碼位於我的“ js / index.js” [創建項目時創建的默認文件]中。

它工作正常,可以帶我到“ page.html”,但是當我嘗試使用“ js / index.js”中包含的相同功能(我在2頁中使用相同的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');
                        }
                };

並感謝您的幫助:)

我不確定,但是您可以嘗試代替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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM