Why does Phantom.js not work with my Backbone application?

page.open(My_url, function(status) { page.evaluate(function() { var html = document.documentElement.outerHTML; return html; }, function(result){ res.send(result); }); }); 

I have a backbone app. "My_url" points to this main page.

When I run this code, outerHTML does not include rendered views. It has only the main owners of css / scripts / container.

It, like the Backbone application, did not start at all. How do I evaluate () to return a fully processed Backbone application (after all ajax calls and more)?

In the end what is the point of Phantom.js

+4
source share
1 answer

It is not always enough to evaluate the page immediately after loading it. Sometimes, due to the way JavaScript is written on the page, you will have to wait and allow the execution of the JavaScript page. To this end, I suggest you try and add expectation before completing the assessment. eg.

 function ( status ) { if ( status === 'fail' ) { phantom.exit( 1 ); return; // essential, if you don't, below will be executed } window.setTimeout( function () { var result = page.evaluate( function () { var html = document.documentElement.outerHTML; return html; } ); res.send( result ); }, 1000 // wait 1,000ms (1s) ); } 

If you find that you need to wait, then you can wait for something. To this end, find waitFor or a similar function on the Internet that allows your script to continuously poll until a specific DOM element appears.

+6
source

Source: https://habr.com/ru/post/1482322/


All Articles