title: phantomJS - Pass Argument to the JS File
body: <p>Right now I'm using the following command to run <code>phantomJS</code></p>
  
  <pre><code>exec('./phantomjs table.js',$op,$er);
  </code></pre>
  
  <h3>table.js</h3>
  
  <pre><code>var page = require('webpage').create();
  page.open('table.php', function () {
      page.render('table.png');
      phantom.exit();
  });
  </code></pre>
  
  <p>This serves the purpose. But now I'm required to work with a dynamic variable, namely <code>date</code>. So is it possible to pass a PHP or Javascript variable inside the <code>exec</code> command line so that I can use that variable inside <code>table.js</code>?</p>
  
  <h3>Update</h3>
  
  <p>I tried modifying my code according to a solution posted here <a href="https://stackoverflow.com/questions/16752882/passing-a-variable-to-phantomjs-via-exec?lq=1">Passing a variable to PhantomJS via exec</a></p>
  
  <pre><code>exec('./phantomjs table.js http://www.yahoo.com',$op,$er);
  </code></pre>
  
  <h3>table.js</h3>
  
  <pre><code>var args = require('system').args;
  var page = require('webpage').create();
  var address = system.args[1];
      page.open(address, function () {
          page.render('table.png');
          phantom.exit();
      });
  </code></pre>
  
  <p>But this results in 2 problems:</p>
  
  <ul>
  <li>The whole process takes about 3-4 minutes to finish</li>
  <li>After that I get "Server Not Found" message</li>
  </ul>
  
  <p>If I remove the modified code, everything works as expected.</p>
  
  <h3>More Debugging</h3>
  
  <p>Inside <strong>table.js</strong> I used this:</p>
  
  <pre><code>var args = require('system').args;
  args.forEach(function(arg, i) {
  
      console.log(i+'::'+arg);
  
  });
  
  var page = require('webpage').create();
  var address = 'http://www.gmail.com';
  page.open(address, function () {
      page.render('github.png');
      phantom.exit();
  });
  </code></pre>
  
  <p>On running this, my <code>$op</code> (from <code>exec</code> command) printout out this:</p>
  
  <pre><code>Array ( [0] =&gt; 0::table.js [1] =&gt; 1::http://www.yahoo.com )
  </code></pre>
  
  <p>So far so good. But as soon as I put the below code, the same problems are encountered</p>
  
  <pre><code>var args = require('system').args;
  
  var page = require('webpage').create();
  var address = system.args[1]; // &lt;--- This line is creating problem, the culprit
  page.open(address, function () {
      page.render('github.png');
      phantom.exit();
  }); 
  </code></pre>
  
  <p>Seems like that is not the correct syntax. Anything obvious that I'm unable to see?</p>