简体   繁体   中英

Getting undefined when using Cordova plugin device

I wrote a small piece of code to get all information about a user's device using the cordova-plugin-device plugin. But it does not return any result when i click a button. But when I click the back button on the browser, i get the error "undefined".

Html file

<div class="ui-grid-b">
     <div class="ui-block-a">
          <label for="kuva"></label>
           <input type="submit" class="ui-btn" onclick="otaKuvaKirjasto();" data-icon="gear" data-iconpos="right" name="kuva" value="Kuva kuvakansiosta"  />
      </div>
      <div class="ui-block-b">
           <input type="submit" class="ui-btn" onclick="otaKuvaKameralla();" data-icon="gear" data-iconpos="right" name="kuva" value="Kuva kameralla"  />
      </div>
      <div class="ui-block-c">
           <input type="submit" class="ui-btn" onclick="naytaLaiteTiedot();" data-icon="gear" data-iconpos="right" name="kuva" value="Laitteen tiedot"  />
      </div>
</div>    
<div class="ui-grid-a">
     <p id="model"></p>
     <p id="platform"></p>
     <p id="version"></p>
     <p id="name"></p>    
 </div>

Javascript code

   document.addEventListener("deviceReady", onDeviceReady, false);

    function onDeviceReady() {

        /*destinationtype = navigator.Camera.destinationType;
        sourcetype = navigator.Camera.PictureSourceType;
        laiteKesken.resolve();*/
        var model = device.model;
        var platform = device.platform;
        var version = device.version;
        var name = device.name;
        naytaLaiteTiedot(model, platform, version, name);

    }

    function naytaLaiteTiedot(model, platform, version, name) {
        $("#model").append("Modeli: "+ model);
        $("#platform").append("Käyttöjärjestelmä: "+ platform);
        $("#version").append("Versio: "+ version);
        $("#name").append("Nimi: "+name);
    }

Device plugin already installed in my project and javascript code i wrote under cordova.js calling row.

Your on click function calls the naytaLaiteTiedot function with no parameters. You should call another intermediate function to collect and pass the parameters to naytaLaiteTiedot .

You can also simpilfy your function this way

function naytaLaiteTiedot() {
    $("#model").append("Modeli: "+ device.model);
    $("#platform").append("Käyttöjärjestelmä: "+ device.platform);
    $("#version").append("Versio: "+ device.version);
    $("#name").append("Nimi: "+device.name);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM