Domanda

I am creating an HTML file for use with OpenWrt LuCI web interface. As discussed here: http://luci.subsignal.org/trac/wiki/Documentation/Templates I am using the Lua Markup language to run a Lua function called runDiag and I need to pass the javascript variable option to the runDiag function. I can't figure out how to make this work. I have tried various modifications to the markup inside the displayDiag function without success.

Can anyone help?

Example:

<%-
    function runDiag(option)
        return option
    end
-%>

<script>
    function displayDiag() {
        var option = document.getElementById('iface').value;
        document.getElementById('diag_text').innerHTML = '<%- write (runDiag(option)) -%>';
    }
</script>
È stato utile?

Soluzione

You can't do this. The Lua template is ran on the server, and the JavaScript code is ran on the client (i.e. web browser). They can't communicate.

The Lua code simply generates an HTML file to send to the client. It doesn't know about JavaScript; it's just some text that it's giving to the client. Here, option refers to a nonexistant Lua variable, which has the value of nil.

Conversely, the JavaScript code has no knowledge of the server-side Lua code. It just gets whatever the server generated. Thus, it only sees this line:

document.getElementById('diag_text').innerHTML = 'nil';

To communicate with the web server, you will need use AJAX or some other protocol.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top