【发布时间】: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 版本?请注册
onConsoleMessage、onError、onResourceError、onResourceTimeout活动 (Example)。可能有错误。
标签: javascript jquery phantomjs referenceerror