title: Detect JavaScript within a page loaded in PhantomJS
body: <p>I'm using PhantomJS as a crawler; if there is no JS in a page I can assume that it's completely loaded when <code>onLoadFinished</code> fires, but if there is JS in a page, I need to wait a bit to give the scripts a chance to do stuff.  This is my current stab at detecting JS:</p>
  
  <pre><code>var pageHasJS = page.evaluate(function () {
      return (document.getElementsByTagName("script").length &gt; 0 ||
              document.evaluate("count(//@*[starts-with(name(), 'on')])",
                                document, null, XPathResult.NUMBER_TYPE,
                                null).numberValue &gt; 0);
  })
  </code></pre>
  
  <p>This looks for <code>&lt;script&gt;</code> tags and for elements with an <code>onsomething</code> attribute.</p>
  
  <p>Q1: Is there any other HTML construct that can sneak JS into a page?  <code>javascript:</code> URLs do not count, because nothing will ever get clicked.<br>
  Q2: Is there a better way to do the second test?  I <em>believe</em> it is not possible to do that with <code>querySelector</code>, hence resorting to XPath, but maybe there is some other feature that would accomplish the same task.<br>
  Q3: The crawler does not interact with the page once it is loaded.  The <code>onload</code> event is the only legacy event attribute <em>that I know of</em> that fires in the absence of user interaction.  Are there any others?  In other words, would it be safe to replace the second test with <code>document.evaluate("count(//@onload)", ...)</code> or maybe even <code>!!document.body.getAttribute("onload")</code>?</p>