BarcodeScanner2 = TypeError: Cannot set property 'innerHTML' of null

后端 未结 2 1460
既然无缘
既然无缘 2021-01-16 12:13

I am trying to develop a barcode scanning app using phonegap-1.4.1 in android. I am trying to store all the values in an array code[] and later on I am displayi

2条回答
  •  一生所求
    2021-01-16 12:40

    Add below javascript Function in your html file ( top of the page).

    function SaveDataToLocalStorage(barcodeValue)
    {
    
        var oldItems = JSON.parse(localStorage.getItem('barcodes')) || [];
        var newItem = {
            'barcode': barcodeValue
        };
        oldItems.push(newItem);
        localStorage.setItem('barcodes', JSON.stringify(oldItems));
    
    }
    

    Now Change Your barcode Scan Code as below :

    var scanCode = function () {
          window.plugins.barcodeScanner.scan(function (result) {
             if(result.cancelled == true ) {
               window.location.href = 'page5.html';
             } else {
               // below function save your data in localstorage.
               SaveDataToLocalStorage(result.text);
               scanCode();
             }
    
         }, function (error) {
                alert("Scan failed: " + error);
               window.location.href = 'page5.html';
        });
      }
    

    Now, if you want to display all barcodes in page5.html just read all barcodes from localstorage and display it in page.

    use following function page5.html to display all barcodes in page5.html

    function displayValues()
    {
        var oldItems = JSON.parse(localStorage.getItem('barcodes')) || [];
    
        for(var i=oldItems.length-1;i>=0;i--)
        {
            var html=document.getElementById("allCodes").innerHTML;
            document.getElementById("allCodes").innerHTML=html+"
    "+oldItems[i].barcode; } }

    make one div name allCodes in page5.html

    Your page5.html

    
    

    //Display in table :

    function displayValues()
    {
        var oldItems = JSON.parse(localStorage.getItem('barcodes')) || [];
    
        if(oldItems.length>0)
        {
            document.getElementById("allCodes").innerHTML="";
        }
        for(var i=oldItems.length-1;i>=0;i--)
        {
            var html=document.getElementById("allCodes").innerHTML;
            html=html+"";
            document.getElementById("allCodes").innerHTML=html;   
        }    
        if(oldItems.length>0)
        {
            var old=document.getElementById("allCodes").innerHTML;
            document.getElementById("allCodes").innerHTML=old+"
    "+oldItems[i].barcode+"
    " } }

提交回复
热议问题