Billy Pilgrim Billy Pilgrim - 1 year ago 119
HTML Question

How do I call a Lua script from an HTML5 script/file/page

Very basic question (I think), but I'm new to this web/scripting world.

I want to create web pages with dynamic content...

If I have an HTML page (HTML5) and want to call a lua script from it, how do I:

a) invoke it?

<script type=text/application>
<script type=text/lua>
etc? This must be so basic that I couldn't find an answer.

b) retrieve data from it? Or can I? Can I do something like:
int xx = 0;
<script type=text/lua>

xx = 123;

and have any hope that xx will be 123 when the script exits?

c) replace the current web page with the content generated by the lua script.

Sorry for such remedial questions, but all help is greatly appreciated.


Answer Source

On the WWW scripts can run in two places.

  1. In the web browser
  2. On the web server

If you want it to run in the browser, then you need support for the language built into the browser (or provided by an extension). For all practical purposes, if you are writing webpages for the WWW, then the only language you can use in an HTML <script> is JavaScript.

If you want to run it on the web server, then you need to get your HTTPD to run the script in response to a URL being requested from it. The simplest way to achieve this is via CGI.

With CGI, the HTTPD will run a program (as a separate process) in response to the request being made. It will pass in various information about the request via STDIN and environment variables (as described in the CGI specification). The script then prints an HTTP response (header (at least a Content-Type) and body (e.g. an HTML document)) and sends it to STDOUT where the HTTPD picks it up and sends it back to the browser.

How you configure your server to run programs using CGI depends on the server. Apache has a guide for their server.

There are probably CGI libraries for Lua, but I don't know the language so cannot make any recommendations.

CGI is a slow and inefficient protocol (as it requires a new processes to be spawned for each request). There are alternatives, such as FastCGI and various language specific options. Again, I don't know what is considered optimal in Lua land.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download