Home » Android » javascript – "Uncaught SyntaxError: Unexpected identifier"

javascript – "Uncaught SyntaxError: Unexpected identifier"

Posted by: admin June 15, 2020 Leave a comment

Questions:

I know this question asked many times but I tried to find solution but didn’t get from available SO questions.

I am very newbie on Javascript. I am trying to create sample calculation application in android using cordova. For that, I created cordova plugin. But I am getting two issues continuously.

"Uncaught SyntaxError: Unexpected identifier", source: file:///android_asset/www/js/index.js (36)

here is index.java code and error targetting performCalculation() first line.

    var app = {

// Application Constructor
initialize: function() {
    this.bindEvents();
},
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
    document.getElementById("btnCalculate").addEventListener("click", performCalculation);
},
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);
}

performCalculation: function (){
    console.log("in index.html");
    var success = function() {
        alert("Success");
    };
    var error = function(message) {
    alert("Oopsie! " + message);
    };
    performAddition(20,10,success,error);
}

};  
app.initialize();

Here is my second exception which I am getting.

"Uncaught SyntaxError: Unexpected token .", source: file:///android_asset/www/js/calculation.js (3)

and Here is code of calculation.js

var calculationPlugin = {
console.log("calculation");
    performAddition: function(first_number, second_number, successCallback, errorCallback) {
    console.log("addition");
        cordova.exec(
            successCallback, // success callback function
            errorCallback, // error callback function
            'CalculationPlugin', // mapped to our native Java class called "CalculationPlugin"
            'addition', // with this action name
            [{                  // and this array of custom arguments to create our entry
                "firstNumber": first_number,
                "secondNumber": second_number,

            }]
        );
     }
}
How to&Answers:

First syntax error

You are missing “,” after the receivedEvent function.

Second syntax error

Calculation plugin is an object, as you have the console in it, the
error is thrown. Remove the console from that object.

Answer:

should you change this: app.receivedEvent(‘deviceready’); to this.receivedEvent(‘deviceready’);

And you only have syntax error, It would be helpful if you put code line number there.