TypeError:undefined在注入javascript时不是gwt中的函数

时间:2014-09-12 05:02:05

标签: javascript gwt

当我尝试通过ScriptInjector.fromString()在我的GWT项目中注入此javascript代码时会抛出错误

TypeError: undefined is not a function 这是我的代码:

 (function(window) {
    var $canvas,
        onResize = function(event) {
          $canvas.attr({
            height: window.innerHeight,
            width: window.innerWidth
          });
        };

    $(document).ready(function() {
      $canvas = $('canvas');
      window.addEventListener('orientationchange', onResize, false);
      window.addEventListener('resize', onResize, false);
      onResize();

      $('form').signaturePad({
        drawOnly: true,
        defaultAction: 'drawIt',
        validateFields: false,
        lineWidth: 0,
        output: null,
        sigNav: null,
        name: null,
        typed: null,
        clear: 'input[type=reset]',
        typeIt: null,
        drawIt: null,
        typeItDesc: null,
        drawItDesc: null
      });
    });
  }(this));

在我在inspect元素中找到的gwt自己的文件中注入后的最终javascript如下所示:

function com_google_gwt_core_client_ScriptInjector$FromString_ScriptInjector$FromString__Ljava_lang_String_2V(){
  this.com_google_gwt_core_client_ScriptInjector$FromString_scriptBody = "(function(window) {var $canvas,onResize = function(event) {}; $(document).ready(function() {  $canvas = $('canvas'); window.addEventListener('orientationchange', onResize, false);window.addEventListener('resize', onResize, false);onResize();  $('form').signaturePad({drawOnly: true, defaultAction: 'drawIt', validateFields: false,lineWidth: 0, output: null,sigNav: null,name: null,typed: null,clear: 'input[type=reset]',typeIt: null,drawIt: null,typeItDesc: null,drawItDesc: null})})}(this));";
 }

任何帮助都将受到高度赞赏。谢谢

1 个答案:

答案 0 :(得分:0)

我敢打赌你在错误的窗口中注入你的脚本并且那里没有定义$

大多数情况下,您希望在加载*.nocache.js脚本的页面中注入脚本,而不是实际运行的一个(隐藏的iframe)GWT代码,所以不要忘记告诉它{ {1}}:

ScriptInjector