Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在PhantomJS中,为什么jQuery调用会导致脚本挂起?_Javascript_Jquery_Phantomjs - Fatal编程技术网

Javascript 在PhantomJS中,为什么jQuery调用会导致脚本挂起?

Javascript 在PhantomJS中,为什么jQuery调用会导致脚本挂起?,javascript,jquery,phantomjs,Javascript,Jquery,Phantomjs,我正在Mac OS X Yosemite上使用PhantomJS 2.0.0: $ phantomjs --version 2.0.0 如下图所示,我的脚本似乎挂在调用$('h1').size()的行上: system = require('system'); function usage() { console.log("usage: phantomjs " + system.args[0] + " <url>"); phantom.exit(1); } consol

我正在Mac OS X Yosemite上使用PhantomJS 2.0.0:

$ phantomjs --version
2.0.0
如下图所示,我的脚本似乎挂在调用
$('h1').size()
的行上:

system = require('system');

function usage() {
  console.log("usage: phantomjs " + system.args[0] + " <url>");
  phantom.exit(1);
}

console.log("system.args.length=" + system.args.length);
if (system.args.length != 2) {
  console.log("usage bad....");
  usage();
} else {
  var url = system.args[1];
  var page = require('webpage').create();

  console.log("Opening page: " + url);
  page.open(url, function (status) {
      if (status !== "success") {
        console.log("Unable to access network");
      } else {
        console.log("Setting timeout...");
        window.setTimeout(function() {
          page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js", function() {
            console.log("Searching for Seawolf Calendar...");
            console.log("h1.size=" + $('h1').size());

            console.log("Exiting with status 0...");
            phantom.exit(0);
          });
        }, 5000);
      }
  });
}
输出如下:

system.args.length=2 Opening page: http://www.sonoma.edu/calendar/groups/clubs.html Setting timeout... Searching for Seawolf Calendar... [Hung ...] system.args.length=2 第一页:http://www.sonoma.edu/calendar/groups/clubs.html 正在设置超时。。。 正在搜索海狼日历。。。 [挂…]
为什么jQuery调用会挂起脚本?

PhantomJS 2.0.0由于某种原因没有显示任何错误(这是一个已知的错误)

错误是,
$
不是一个函数。如果jQuery存在于页面中,那么您可以在页面中使用它,但它在页面上下文之外(在
page.evaluate()
)无法工作

只能通过
page.evaluate()
访问DOM/page上下文:

请注意,您不能在
页面内使用任何外部变量。evaluate()
,因为它是沙盒。报告说:

注意:参数和
evaluate
函数的返回值必须是一个简单的原语对象。经验法则:如果可以通过JSON对其进行序列化,那么就可以了

闭包、函数、DOM节点等将无法工作


我刚从page.evaluate()返回数组时遇到问题。也许这也是一个bug?考虑到我答案的最后一部分,这可能是预期的行为。如果它是一个实际的数组,如
[1,2,3]
中所示,那么这不应该是一个问题。请记住,
$(选择器)
是一个只模仿JavaScript数组行为的jQuery集合。它(和它的内容)不能被序列化以便在外部传递。我注意到你从未对你得到的答案进行投票。我想知道这是为什么。我的答案是否缺少一些信息,或者在其他方面不好? system.args.length=2 Opening page: http://www.sonoma.edu/calendar/groups/clubs.html Setting timeout... Searching for Seawolf Calendar... [Hung ...]
console.log("h1.size=" + page.evaluate(function(){
    return $('h1').size();
}));