简体   繁体   中英

“Uncaught ReferenceError: cordova is not defined”

I'm trying use the Facebook slider menu on iOS, with the PhoneGap. The problem is that I am not able to insert data in sqlite, When clicking on the save button I got this error:

"Uncaught ReferenceError: cordova is not defined".

This is the source:

<!DOCTYPE html>
<html>
<head>
    <title>Registration Form</title>
    <link rel="stylesheet" type="text/css" href="../../css/index.css" />
    <script type="text/javascript" src="../../js/SQLitePlugin.js"></script>
    <script type="text/javascript" charset="utf-8">
        // Wait for Cordova to load
    //
   document.addEventListener("deviceready", onDeviceReady, false);

   function onDeviceReady() {
     console.log("device ready");
   }

   function insert() {
   console.log("Run1");
   var db = window.sqlitePlugin.openDatabase({
     name: "PHR.db"
   });
   db.transaction(function(tx) {
     tx.executeSql(
       'CREATE TABLE IF NOT EXISTS SignUp (firstname VARCHAR,lastname VARCHAR,email VARCHAR, password VARCHAR ,question1 VARCHAR, answer1 VARCHAR,question2 VARCHAR,answer2 VARCHAR, question3 VARCHAR,answer3 VARCHAR)'
     );
   });
   db.transaction(function(tx) {
     var fname = document.getElementById("fn").value;
     var lname = document.getElementById("ln").value;
     var emai = document.getElementById("em").value;
     var passw = document.getElementById("pas").value;
     var qus1 = document.getElementById("qs1").value;
     var ans1 = document.getElementById("as1").value;
     var qus2 = document.getElementById("qs2").value;
     var ans2 = document.getElementById("as2").value;
     var qus3 = document.getElementById("qs3").value;
     var ans3 = document.getElementById("as3").value;
     tx.executeSql(
       'INSERT INTO SignUp (firstname,lastname,email, password ,question1 , answer1 ,question2 ,answer2 , question3,answer3) VALUES (?,?,?,?,?,?,?,?,?,?)', [
         fname, lname, emai, passw, qus1, ans1, qus2, ans2, qus3,
         ans3
       ]);
   }, null);
   });
   }    

    </script>

you need to add

<script type="text/javascript" src="cordova.js"></script>

in the head tag

In your code :

<head>
<title>Registration Form</title>
<link rel="stylesheet" type="text/css" href="../../css/index.css" /> 
<script type="text/javascript" src="cordova.js"></script>  //--> You missed this line.
<script type="text/javascript" src="../../js/SQLitePlugin.js"></script>
<script type="text/javascript" charset="utf-8">
//Remaining code goes here ... ...

在你的html文件中添加cordova脚本,他头

I was in a very tricky situation with reactjs + cordova for mobile. This was specifically happening when trying to leverage cordova's datadirectory field on a mobile device, the prod build would error out.

The solution that worked for me

Simply reference window.cordova instead of cordova

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