忽略空的全球对象的Phonegap

时间:2016-09-09 22:32:42

标签: javascript cordova phonegap-cli

我正在测试一个html页面的phonegap应用。它在我的浏览器中完美运行,但是当我通过PhoneGap CLI在手机上测试时,它完全失败了。我把它设置为Weinre,但它不像浏览器那样在控制台中显示错误。无论如何。我把它缩小到这样一个事实,即我在js开头定义的全局空对象被视为未定义,所以当我尝试添加它时,代码只是拒绝启动循环。在Chrome中,日志被正确定义为一个对象并且运行正常。

这是我的代码:

var holding_layers_info = {};

function my_function() {
    console.log(typeof holding_layers_info); // this logs undefined on PhoneGap App and object on Chrome log
    // my loop here, adding values to holding_layers_info, which doesn't run because of the undefined object above
}

我对此感到困惑,在网上找不到任何类似的东西,也不知道从哪里开始修复它。使用var holding_layers_info = new Object();定义空对象或仅将其保留为var holding_layers_info;也没有任何区别,不是我认为的那样。

函数中定义的空对象按预期工作,但我需要它是全局的。

编辑:这是我的index.html的删节版本:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
        <!-- This is a wide open CSP declaration. To lock this down for production, see below. -->
        <!-- <meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *" /> -->
        <!-- Good default declaration:
        * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
        * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
        * Disables use of eval() and inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
            * Enable inline JS: add 'unsafe-inline' to default-src
            * Enable eval(): add 'unsafe-eval' to default-src
            * Create your own at http://cspisawesome.com
        -->
        <!-- <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: 'unsafe-inline' https://ssl.gstatic.com; style-src 'self' 'unsafe-inline'; media-src *" /> -->

        <link rel="stylesheet" type="text/css" href="css/ol.css">
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <link rel="stylesheet" type="text/css" href="css/fonts.css" />
        <link rel="stylesheet" type="text/css" href="css/easy-autocomplete.css" />
        <title>La la la</title>
    </head>
    <body>
        <!-- Boring HTML-only page content here -->

        <script src="https://code.jquery.com/jquery-1.12.4.min.js"
            integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
            crossorigin="anonymous"></script>
        <script type="text/javascript" src="js/ol.js"></script>
        <script type="text/javascript" src="js/general.js"></script>
        <script type="text/javascript" src="js/jquery.easy-autocomplete.min.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            app.initialize();
        </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

在我加载的第一个JS脚本上,我确保将此函数放在顶层以进行调试:

//debugging
var debug = true;
if (debug) {
  window.onerror = function (msg, url, linenumber) {
    alert('Error message: ' + msg + '\nURL: ' + url + '\nLine Number: ' + linenumber);
    return true;
  };
}

这基本上会警告通常来自控制台的每个错误。如果这给你一个错误,我认为这将是非常有帮助的!

根据我们现在掌握的信息,我确信没有人能真正回答您的问题。它太模糊了。回到我身边=)

我要把它扔出去,因为你永远不会知道: 您是否忘记删除Phonegap模板附带的onDeviceReady中的index.js功能?

谈论这一部分:

onDeviceReady: function() {
    app.receivedEvent('deviceready');  //You should place YOUR starting function here!
},    
receivedEvent: function(id) {
  var parentElement = document.getElementById(id);
  app.receivedEvent('deviceready');
  var receivedElement = parentElement.querySelector('.received');//Quite sure you removed this element from the DOM :)

  listeningElement.setAttribute('style', 'display:none;');
  receivedElement.setAttribute('style', 'display:block;');

  console.log('Received Event: ' + id);
}

你知道,这部分是选择你可能删除的DOM元素。我注意到在我的项目中,当你在不存在的元素上触发某些东西时,chrome不会再杀掉你的JS。

从那里调用你自己的(Starting)函数会有点像这样:

onDeviceReady: function() {
   MyInitFunction();
}

- 您可以删除其余部分。 (整个receivedEvent:函数)

在一个小旁注上,我现在看到你正在使用jQuery 1.x.由于您正在创建移动应用,因此您不必担心旧版浏览器的向后兼容性。我目前正在使用2.x,但我认为切换到新的更快(更快)3.x