【问题标题】:PhantomJS ReferenceError for included function包含函数的 PhantomJS ReferenceError
【发布时间】:2016-10-11 14:30:50
【问题描述】:

我正在尝试渲染我的 web 应用程序的页面。

<html>
  <head>
   <script type="text/javascript" src="jquery.js"></script>
   <script type="text/javascript" src="includes.js"></script>
   <script type="text/javascript">
    jQuery(document).ready(function(){
      load_gantt(some, values);
    });
   </script>
  </head>
  <body>
   <div class="container"></div>
  </body>
</html>

如您所见,我包含了 jQuery 和一个已连接的 javascript 文件,其中包含我所有的库和自定义脚本。它被合并到一个文件中,以确保在自定义代码启动时依赖库已准备就绪。可能不漂亮或不理想,但这不是问题所在。

问题是我调用 load_gantt() 的脚本块。该函数定义在 include.js 的末尾(在所有库之后),如下所示:

load_gantt = function(some, values){ /* CODE HERE */ };

load_gantt() 对 div.container 进行实际渲染,这非常重要。当我现在尝试用 PhantomJS 渲染这个文件时,我得到了这个错误:

ReferenceError: Can't find variable: load_gantt

我还尝试删除脚本块并使用 phantomJS 的评估函数来运行 load_gantt() 但它会导致相同的错误。看起来实际HTML页面的范围与包含的js文件的范围不同。然而,奇怪的是,phantomjs 并没有抱怨脚本块的 jQuery 部分,尽管 jquery 库以相同的方式被包含...

顺便说一句,代码在每个浏览器中都能完美运行。没有问题或错误...

编辑:修正了一些小的语法错误

【问题讨论】:

  • 您的load_gantt 变量是全局变量吗?或者它是否位于您的 includes.js 的某个函数范围内?
  • 它是全球性的。正如我所说,代码在浏览器中正常工作。我尝试使用window.load_gantt = function .. 将其附加到window,但这给了我另一个错误..TypeError: undefined is not a constructor (evaluating 'window.load_gantt(...)' - 编辑:我尝试将甘特库(它是一个DHTMLX甘特图)绑定到window,但是错误仍然存​​在。
  • @Max 您使用哪个 PhantomJS 版本?请注册onConsoleMessageonErroronResourceErroronResourceTimeout 活动 (Example)。可能有错误。

标签: javascript jquery phantomjs referenceerror


【解决方案1】:

这不是一个真正的答案,而是一个修复。我注册了 onConsoleMessageonErroronResourceErroronResourceTimeout 事件并注意到和错误表明该函数未正确初始化。

我用过

load_gantt = function(a = 'default_a', b = 'default_b'){ /* ... */};

但是 PhantomJS 不能适应这个......它必须没有默认值。我不知道为什么,但它现在可以工作了。

load_gantt = function(a, b){ /* ... */};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-22
    • 2019-01-14
    • 1970-01-01
    • 2015-05-26
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    相关资源
    最近更新 更多