简体   繁体   中英

How to include multiple JavaScript libs in PhantomJS?

I'm using PhantomJS to generate a series of images based on the state of an HTML canvas element which I am manipulating via JS. This canvas manipulation depends on 3 separate JS libraries and some inline scripts which I invoke during window.onload .

The PhantomJS docs cover how to include a single JS library, but it doesn't cover how to include several.

Can anyone provide me with the correct syntax to include several JS libs, and then run some scripts during window.onload ?

There is a callback for page.includeJs() . When the first one is done, then you can load the next one. This is usually done recursively.

function multipleIncludeJs(page, jsArray, done) {
    if (jsArray.length === 0) {
        done();
        return;
    }

    var url = jsArray.shift();
    page.includeJs(url, function(){
        multipleIncludeJs(page, jsArray, done);
    });
}

and use it like this:

multipleIncludeJs(page, ["http://code.jquery...", "http://getbootstrap..."], function(){
    console.log("loaded");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM