在phantomjs中打开页面外定义页面评估 [英] define page evaluate outside of page open in phantomjs

查看:54
本文介绍了在phantomjs中打开页面外定义页面评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在打开带有phantomjs的网页的基本示例中,我们在下面的代码中使用open web并评估函数中页面打开时的完成情况。

in the basic example of open a web page with phantomjs we use below code for open web and evaluate when page open complete in a function .

var page = require('webpage').create();
page.open('http://www.sample.com', function() {
  page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
    page.evaluate(function() {
           console.log(document.title);
    });
    phantom.exit()
  });
});

是让我们在page.open回调函数的函数外定义page.evaluate的任何方式我们需要时随时打电话,打开页面之后不会打电话

is any way that let us define page.evaluate in a function out side of page.open callback function for call it any time we need and no just after page open

推荐答案

不确定你究竟是什么意思,但是从我的意思来说从你的例子中可以理解这可能有所帮助:

not sure what exactly do you mean, but from what I've understood from your example this could help maybe:

var page = require('webpage').create();
// document is already initialized
document.title = 'internal call';

page.onConsoleMessage = function (msg, lineNum, sourceId) {
    console.log('PAGE\'S CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};

var func = function () {
    console.log('Title: ', document.title);
}

// calling outside of the page.open:
func();

page.open('http://google.com/', function () {
    page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function () {
        // calling inside:
        page.evaluate(func);
        page.close();
        phantom.exit(0);
    });
});

还有 page.evaluate 关于参数和闭包的函数

Also there is a Note for page.evaluate function about arguments and closures

这篇关于在phantomjs中打开页面外定义页面评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆