Javascript 未捕获引用错误自定义事件未在文件ratchet中定义

Javascript 未捕获引用错误自定义事件未在文件ratchet中定义,javascript,cordova,ratchet,push.js,Javascript,Cordova,Ratchet,Push.js,我是phonegap和ratchet框架的新手。我正在尝试用push.js加载外部脚本。这是我的js文件内容 function init(){ document.addEventListener('deviceready', onDeviceReady, false); function onDeviceReady(){ alert("device ready for use"); } var checkPage = functi

我是phonegap和ratchet框架的新手。我正在尝试用push.js加载外部脚本。这是我的js文件内容

function init(){
    document.addEventListener('deviceready', onDeviceReady, false);

    function onDeviceReady(){

        alert("device ready for use");
        }
       var checkPage = function(){

        alert("push");
        var scriptName; 
        var scriptsList = document.querySelectorAll('script.js-custom');  // Add a "js-custom" class to your script tag
        for (var i = 0; i<scriptsList.length;i++) {

            // Handle scripts in separate files by assigning the script file name to its id.
            // We save it in a variable because the ".done" callback is asynchronous.
            scriptName = scriptsList[i].id;  // IMPORTANT: Only one loadable script per page!
            $.getScript("js/" + scriptName)
              .done(function (script, textStatus) {
                  eval(script);
                  alert("ok");
              })
              .fail(function(){
                  alert("error");
              });

        }


    };   

    window.addEventListener('push', checkPage);

    } 

请帮助我。

您的第二个html文件中没有cordova。加载two.html时,浏览器会销毁上一页以及与之关联的所有脚本。您应该在单个页面中完成所有操作,我建议使用mvvm框架,如angularjs、emberjs等


所以,确切地说,您的第二个html没有cordova,因此不会为该文件触发DeviceRady。但是,对于第一个html,您有DeviceRady和ratchet引用。但是,在这里,您没有js custom的sample.js引用。因此,在deviceready内部(在第一个html中),ratchet抱怨缺少js自定义引用。

这是因为WebView实现中缺少window.CustomEvent

添加此Polyfill,它每次都有效。我在Mozilla开发者网络上找到了解决方案:

/*
 * Polyfill for adding CustomEvent
 * see : https://developer.mozilla.org/fr/docs/Web/API/CustomEvent
 */

if (!window.CustomEvent) { // Create only if it doesn't exist
    (function () {
        function CustomEvent ( event, params ) {
            params = params || { bubbles: false, cancelable: false, detail: undefined };
            var evt = document.createEvent( 'CustomEvent' );
            evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
            return evt;
        };

        CustomEvent.prototype = window.Event.prototype;

        window.CustomEvent = CustomEvent;
    })();
}

但我在设备中加载了一些时候工作正常,有时不先添加cordova。然后Ratchet请阅读我的答案,我更新并指出了代码中的确切问题(我想)。使用data ignore=“push”可以很好地工作,但我需要自定义脚本来推动。你能给出答案吗?()
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Notes</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">

    <!-- Roboto
    <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:400,500,700"> -->

    <link rel="stylesheet" href="ratchet.min.css">
    <script src="ratchet.min.js"></script>
  </head>
  <body>
      <header class="bar bar-nav">
  <h1 class="title">Two</h1>
    </header>
  </body>
  <div class="content">
  <script src="js/sample.js" class="js-custom" id="sample.js"></script>
  </div>
</html>
Uncaught ReferenceError: CustomEvent is not defined at file:///android_asset/www/js/ratchet.min.js:10
/*
 * Polyfill for adding CustomEvent
 * see : https://developer.mozilla.org/fr/docs/Web/API/CustomEvent
 */

if (!window.CustomEvent) { // Create only if it doesn't exist
    (function () {
        function CustomEvent ( event, params ) {
            params = params || { bubbles: false, cancelable: false, detail: undefined };
            var evt = document.createEvent( 'CustomEvent' );
            evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
            return evt;
        };

        CustomEvent.prototype = window.Event.prototype;

        window.CustomEvent = CustomEvent;
    })();
}