Sunday, February 4, 2018

Include multiple JavaScript before the code runs in PhantomJS?

Leave a Comment

I want to include multiple javascript in PhantomJS. page.includeJS / page.injectJS does not show up option to include multiple scripts. Can you provide a working example for the same?

Code:

page.open('index.html', function (openStatus) {   console.log("OPen Status is "+openStatus)   if(openStatus == 'success') {     page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', function(jStatus) {       console.log("Jquery status: "+jStatus)       (page.includeJs('jquery.textillate.js', function(tStatus) {         console.log("Text status: "+tStatus)         (page.evaluate(function () {           var tag = document.getElementById("main-tag")           tag.innerHTML = "Hello World"           document.getElementById("main-tag").textillate({              maxDisplayTime: 2000,             in: { effect: 'fadeInUp',             delay: 7000 }            });           console.log('Textilate is: '+tag)          }))       }))     })     // setInterval(function() {     //   page.render('/dev/stdout', { format: "png" });     //   if( frames == 0 ){     //       phantom.exit();     //   }     //   frames--;     // }, 40);   } }); 

Error: undefined is not a constructor (evaluating 'document.getElementById("main-tag").textillate({

1 Answers

Answers 1

undefined is not a constructor means that you're calling a method that is not defined. Most probably jquery.textillate.js isn't available to the target page, because judging by the code samle it's a local script, and for that to use in page.evaluate you should use page.injectJs method.

To also answer another question of yours: there's a simpler way to inject multiple scripts. Just put them all in one bundled local script and inject it.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment