小编Him*_*lot的帖子

JQuery没有被包含在PhantomJs中

我试图使用jquery与phantomjs.我尝试了一个独立的例子,它运行良好.这是我做的:

  var page = require('webpage').create();
  page.open("http://www.phantomjs.org", function(status) {

    page.onConsoleMessage = function(msg) {
      console.log("message recvd: " + msg);
    };

    var result;

    page.includeJs("https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js", function() {
      console.log("loading jquery");
    });

    setTimeout(function() {
      page.evaluate(function() {
        console.log("$(\"title\").text() -> " + $("title").text());
      });
    }, 1000);
 }
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

loading jquery
message recvd: $("title").text() -> PhantomJS | PhantomJS
Run Code Online (Sandbox Code Playgroud)

在上面的代码片段中,我在evaluate函数上使用了setTimeout(),因为includeJs()将异步执行并需要一些时间来加载jquery.如果我不使用setTimeout()或使用较小的值进行超时,则它不起作用.

但是,当我在我的应用程序中尝试相同的代码时,它不起作用.这是我有的:

var baseSetup = function(guid, page, config, request, response) {
  /* enable the console output inside the callback functions */
  page.onConsoleMessage = function (msg) {
    console.log(guid + ": console msg: …
Run Code Online (Sandbox Code Playgroud)

javascript jquery phantomjs

5
推荐指数
1
解决办法
4755
查看次数

标签 统计

javascript ×1

jquery ×1

phantomjs ×1