最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Running Javascript file on the server using crontab - Stack Overflow

programmeradmin1浏览0评论

I have a JavaScript file which does some processing and calls a php file in the end. I need to do this entire processing on the server using a cron tab.

Can I write my JavaScript file as

<?php
    echo '<script type="text/javascript"> // JS code ... </script>';
?>

and then use crontab to run the php file?

Or

Should I use node.js to run my JavaScript file on the server using cron tab?

I have a JavaScript file which does some processing and calls a php file in the end. I need to do this entire processing on the server using a cron tab.

Can I write my JavaScript file as

<?php
    echo '<script type="text/javascript"> // JS code ... </script>';
?>

and then use crontab to run the php file?

Or

Should I use node.js to run my JavaScript file on the server using cron tab?

Share Improve this question edited Nov 10, 2016 at 15:27 mister martin 6,2524 gold badges33 silver badges63 bronze badges asked Nov 10, 2016 at 15:17 AAAAAA 472 silver badges12 bronze badges 10
  • javascript is executed by the client (browser) not the server. – mister martin Commented Nov 10, 2016 at 15:19
  • Use node.js, as you said. Javscript runs in the browser which obviously won't suit you trying to run it on a server. – Reinstate Monica Cellio Commented Nov 10, 2016 at 15:20
  • @mistermartin I need to run this my code (which is in javascript and call a php file) every 30 minutes in the background. That's why thought of using a cronJob. – AAA Commented Nov 10, 2016 at 15:23
  • @AAA I'm not familiar with node.js, so I added that to your question tags. I do know javascript will not execute from cron as you're suggesting. – mister martin Commented Nov 10, 2016 at 15:30
  • @Archer My concern is - my java script file sends some data to a php file. The php file then sends the data over to a server. Is this all possible together if I deal with nodeJS? – AAA Commented Nov 10, 2016 at 15:43
 |  Show 5 more ments

2 Answers 2

Reset to default 3

After OP clarifications, what you need is a headless browser (kind of browser emulator to be run by a machine, not a human), to run your client Javascript code (with your xmlHttp requests and so).

You can find a list of headless browsers here

  • HtmlUnit - Java. Custom browser engine. Limited JavaScript support/DOM emulated. Open source.
  • Ghost - Python only. WebKit-based. Full JavaScript support. Open source.
  • Twill - Python/mand line. Custom browser engine. No JavaScript. Open source.
  • PhantomJS - Command line/all platforms. WebKit-based. Full JavaScript support. Open source.
  • Awesomium - C++/.NET/all platforms. Chromium-based. Full JavaScript support. Commercial/free.
  • SimpleBrowser - .NET 4/C#. Custom browser engine. No JavaScript support. Open source.
  • ZombieJS - Node.js. Custom browser engine. JavaScript support/emulated DOM. Open source. Based on jsdom.
  • EnvJS - JavaScript via Java/Rhino. Custom browser engine. JavaScript support/emulated DOM. Open source.
  • Watir-webdriver with headless gem - Ruby via WebDriver. Full JS Support via Browsers (Firefox/Chrome/Safari/IE).
  • Spynner - Python only. PyQT and WebKit.
  • jsdom - Node.js. Custom browser engine. Supports JS via emulated DOM. Open source.
  • TrifleJS - port of PhantomJS using MSIE (Trident) and V8. Open source.
  • ui4j - Pure Java 8 solution. A wrapper library around the JavaFx WebKit Engine incl. headless modes.
  • Chromium Embedded Framework - Full up-to-date embedded version of Chromium with off-screen rendering as needed. C/C++, with .NET wrappers (and other languages). As it is Chromium, it has support for everything. BSD licensed.
  • Selenium WebDriver - Full support for JavaScript via browsers (Firefox, IE, Chrome, Safari, Opera). Officially supported bindings are C#, Java, JavaScript, Haskell, Perl, Ruby, PHP, Python, Objective-C, and R. Unofficial bindings are available for Qt and Go. Open source.

List taken from https://stackoverflow./a/814929/460306

You have to choose if you want to run the javascript code in the server or in the client.

In the first case, you need to use node.js, as it is server-side. If your javascript code must be run in the client, you can install an add-on on your browser to make automatic from time to time (you can also do a program that simulate that requests).

发布评论

评论列表(0)

  1. 暂无评论