0

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>

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I know that's how it reads it and I never expected it to work as written. I was hoping there was some way to somehow get that text value into Lua through some LuCI trick with the "<%" stuff.
There isn't. LuCI, and other template engines, only operate on text files; they don't - and can't - have knowledge of the javascript they're editing, because they see it as just text. You need a communication mechanism like AJAX to interact with the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.