javascript “未捕获的 ReferenceError:cordova 未定义”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24892403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:31:05  来源:igfitidea点击:

“Uncaught ReferenceError: cordova is not defined”

javascriptioscordova

提问by Nag

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:

我正在尝试在 iOS 上使用 Facebook 滑块菜单和 PhoneGap。问题是我无法在 sqlite 中插入数据,单击保存按钮时出现此错误:

"Uncaught ReferenceError: cordova is not defined".

"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>

回答by frank

you need to add

你需要添加

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

in the head tag

在头部标签中

回答by byJeevan

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 ... ...

回答by Saleh Rastani

add the cordova script in your html file, int he head

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

回答by Hymansonkr

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.

我在使用 reactjs + cordova for mobile 时遇到了非常棘手的情况。当尝试在移动设备上利用cordova 的datadirectory 字段时尤其会发生这种情况,prod 构建会出错。

The solution that worked for me

对我有用的解决方案

Simply reference window.cordovainstead of cordova

简单地参考window.cordova而不是cordova