26

I have a backbone app that has a ton of AJAX. (each page load results in 5-6 ajax calls) because I have many views and internal views within those views,

if I run Phantom.js, can I get the final html result?

Final result is what the user sees after all my ajax calls run.

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

4 Answers4

35

PhamtonJs is a headless WebKit with JavaScript API. When crawling a page, PhatomJs will download all dependants requests such as images, js, css... It will also executes JS and ajax calls. However the onLoaded callback is invoked when the page finishes the loading, not when there are no pending requests. So, you have to use a trick for this.

There are two way to solve the problem :

  • Wait until "something" effectively changed in you page (an element). The best example is waitFor.js
  • Wait a few seconds until no requests were received.
Cybermaxs
  • 24,378
  • 8
  • 83
  • 112
  • don't really know for node.js. But waitFor.js is a basic example of what you have to do. I'm sure you will be able to use it easily in your case. – Cybermaxs Feb 11 '13 at 08:32
11

Phantom.js is able to run the javascript in your page, so this should work. There is an article describing how to give crawlers nice versions of javascript sites using Phantom.js.

Be sure to give Phantom.js enough time to complete the ajax calls, as this thread describes.

sbridges
  • 24,960
  • 4
  • 64
  • 71
1

You can use $.active along with waitFor.js script which returns the no of active connection to a server and we need to wait for it to be 0 for all requests to be complete.

More info: jQuery.active function

Community
  • 1
  • 1
Naga Kiran
  • 8,585
  • 5
  • 43
  • 53
0

If you have the access to javascript codes of the website, aside from waitFor() way, I think you can make your ajax requests synchronous. It's sometimes simpler and more accurate.

iplus26
  • 2,518
  • 15
  • 26