How to exit phantom if it hangs on page.open (with example)
<pre><code> var system = require("system");
      var page;

      // user supplied url
      var myurl = system.args[1];
      // var myurl = 'https://waffles.ch/';

      page = require('webpage').create();

      // suppress errors from output
      page.onError = function(msg, trace) {};

      // 5 seconds
      page.settings.resourceTimeout = 5000;

      // page.settings.javascriptEnabled = false;

      page.open(myurl, function(status) {

          //hack for page.open not hooking into phantom.onError
          setTimeout(function() {
              if (status !== "success") {
                  console.log(myurl);
                  phantom.exit();
                  throw new Error("Unable to access network");
              } else {
                  var pageTitle = myurl.replace(/http.*\/\//g, "").replace("www.", "").split("/")[0];
                  var filePath = "img/" + pageTitle + '.jpg';
                  page.render(filePath, {format: 'jpeg', quality: '75'});
                  console.log(filePath);
                  phantom.exit();
              }

          }, 0);
      });
</code></pre>

<p>Using the above code to take screenshots works fine for most webpages.
Running the script through the console or a web app with the url "<a href="https://waffles.ch/" rel="nofollow">https://waffles.ch/</a>", however, causes it to hang infinitely on page.open (i believe). </p>

<p>The reason for this I assume is because this URL contains some JS animation which doesn't stop running (an aeroplane flying across the screen), and it causes Phantom to lock up. Is this a known bug??</p>

<p>I'm quite sure JS causes the hang because if I switch it off with <code>page.settings.javascriptEnabled = false;</code> then the screenshot for the page is rendered without any problems.</p>

<p>I can't realistically switch off javascript to take screenshots for obvious reasons (page.evaluate, redirects etc), so here's my 2 questions.</p>

<p>1.) Is there a way to render a screenshot for a webpage containing animation like <code>waffles.ch</code> without having to switch off javascript?? </p>

<p>2.) If a webpage does hang, on <code>page.open</code> how can I exit phantom and possibly also return an errror??)</p>

<p>Any help/advice would be greatly appreciated. </p>

<p>Phantom version: 2.1.1
OS: Windows 7 64 bit.</p>

<p>Other thing I've tried.(but still hangs for above url)</p>

<p>with try/catch</p>

<pre><code>var system = require("system");
var page;
// user supplied url
var myurl = system.args[1];

var page = require('webpage').create();
page.open(myurl, function (status) {
    try {
        if (status !== "success") {
            console.log("Unable to access network");
            phantom.exit();
        } else {
            //do some stuff with the DOM
            var pageTitle = myurl.replace(/http.*\/\//g, "").replace("www.", "").split("/")[0];
            var filePath = "img/" + pageTitle + '.jpg';
            page.render(filePath, {format: 'jpeg', quality: '75'});
            console.log(filePath);
            phantom.exit();
        }
    } catch (ex) {
        var fullMessage = "\nJAVASCRIPT EXCEPTION";
        fullMessage += "\nMESSAGE: " + ex.toString();
        for (var p in ex) {
            fullMessage += "\n" + p.toUpperCase() + ": " + ex[p];
        }
        console.log(fullMessage);
    }
});


// ******************************
</code></pre>

<p>Using <code>waitfor()</code> function.
<a href="https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js" rel="nofollow">https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js</a></p>

<pre><code>var system = require("system");
var page;
// user supplied url
var myurl = system.args[1];

var page = require('webpage').create();

// suppress errors from output
page.onError = function(msg, trace) {
  console.log("Error occurred" + msg);
  phantom.exit();
};

// 5 seconds
page.settings.resourceTimeout = 5000;

page.open(myurl, function (status) {
    // Check for page load success
    if (status !== "success") {
        console.log("Unable to access network");
        phantom.exit();
    } else {
        waitFor(function() {
            // Check in the page if a specific element is now visible
            return page.evaluate(function() {

                return $("body").is(":visible");
            });
        }, function() {
           console.log("body is visible");
           phantom.exit();
        });
    }
});
</code></pre>