Calling functions from external JS script into the JS script that is executed in capserjs

1,246 views
Skip to first unread message

Ryan Sullivan

unread,
Feb 21, 2012, 1:32:45 AM2/21/12
to casp...@googlegroups.com
Hi,

Is there a way to call functions from an external JS script into the main JS script that is executed in casperJS?

For example, let's say I have an external script called 'external.js':

function myresult() {
    return 'my result';
}

How would I include 'external.js' in my caperjs script so that I can call on the myresult function?

Thanks,
Ryan

Nicolas Perriault

unread,
Feb 21, 2012, 4:19:47 AM2/21/12
to casp...@googlegroups.com
2012/2/21 Ryan Sullivan <sul...@gmail.com>:

> Is there a way to call functions from an external JS script into the main JS
> script that is executed in casperJS?

You should write a module eg:

exports.myresult = function() {
return 'my result';
}

put this file in lib/mymodule.js and in casper you'll be able to
import it like this:

var mymodule = require('./lib/mymodule');
mymodule.myresult();

You can still alternatively use standard phantomjs' injectJs() of course:

phantom.injectJs('any/script/of/yours.js');

Cheers,

--
Nicolas Perriault
http://www.akei.com/http://prendreuncafe.com/
Mobile: +33 660 92 08 67 — Skype: nperriault

Ryan Sullivan

unread,
Feb 21, 2012, 5:21:49 AM2/21/12
to casp...@googlegroups.com
Thank you!  I really appreciate your quick replies.

Syed Safi

unread,
Jul 27, 2015, 1:49:33 PM7/27/15
to CasperJS
thank you..I have a casperjs script of child process which gives result of python script on console...
can I also use above inject or module or there is another to get the value in my main script.
Reply all
Reply to author
Forward
0 new messages