Calls to phonegap.js won't work

2019-07-27 07:31发布

When my button is clicked, nothing happens.

    <button onclick="captureVideo();">Capture Video</button>

I've put phonegap-1.4.1.js in the WWW folder.

I've included <script src="javascript/phonegap-1.4.1.js" type="text/javascript"></script> in my head section.

I did all the supporting functions, as per Phonegap API Docs' example.

<script>
        // Called when capture operation is finished
        //
        function captureSuccess(mediaFiles) {
            var i, len;
            for (i = 0, len = mediaFiles.length; i < len; i += 1) {
                uploadFile(mediaFiles[i]);
            }       
        }

        // Called if something bad happens.
        // 
        function captureError(error) {
            var msg = 'An error occurred during capture: ' + error.code;
            navigator.notification.alert(msg, null, 'Uh oh!');
        }

        // A button will call this function
        //
        function captureVideo() {
            // Launch device video recording application, 
            // allowing user to capture up to 2 video clips
            navigator.device.capture.captureVideo();
        }
        </script>

My other calls to other external js all work fine. Just phonegap-1.4.1.js isn't working. What am I missing here?

EDIT

When I pasted the phonegap js inline, then all my calls to those functions worked fine. So, I've established that the problem was it never loaded from externally. Diviceready alerts will tell me the same thing. But it doesn't MAKE it load. So, the question remains, how can I get the external js to load?

3条回答
做自己的国王
2楼-- · 2019-07-27 08:11

you need to make sure that deviceready event is firing. only after it fires would u be able to make phonegap api calls

document.addEventListener("deviceready", function(){

});
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-07-27 08:23

You need to call document.addEventListener .. to ensure PhoneGap loaded, So Call init() method on your body onload

<body  onload="init()">

and put the below in your head tag

 <script type="text/javascript">
        var onDeviceReady = function() {
            alert("OnDeviceReady fired.");
        };

        function init() {
            document.addEventListener("deviceready", onDeviceReady, true);
        }
    </script>
查看更多
Melony?
4楼-- · 2019-07-27 08:33

The file is here:

I've put phonegap-1.4.1.js in the WWW folder.

But you're including it in a different location:

I've included <script src="javascript/phonegap-1.4.1.js" type="text/javascript"></script> in my head section.

It's not loading because it expects to find the file inside a javascript directory, while you've put it in the www folder. Make a dir "javascript" inside the www folder, or remove "javascript" from the src in your script tag.

查看更多
登录 后发表回答