Add modal window in ejs file

2.3k Views Asked by At

I wrote a little program with node js and I used ejs as a template. In my program, I calculate two parameters 'msg1' and 'msg2' that I want to show on a modal window. Unfortunately I couldn't do that with ejs.

1

There are 1 best solutions below

2
On BEST ANSWER

As I understand it, you're running the .ejs template on the server, not the client.

Anything inside <% %> runs as part of the template, which means it's trying to call alert in the server. This should fail.

You haven't said what msg1 and msg2 are. If they're client-side variables then all you need is:

function alertNumber() {
    alert(msg1 + msg2)
}

which would mean you don't even need templating - it's just an HTML file. On the other hand, if msg1 and msg2 are server-side variables, they need to be inserted using the template. A naive way of doing so would be like this:

function alertNumber() {
    alert('<%- msg1 + msg2 %>')
}

This only works if msg1 + msg2 doesn't contain the characters ', \, newline, carriage return, and possibly others I've missed. If it does, the script will probably fail. In particular, don't do this unless msg1 and msg2 are from a trusted source, because whoever controls them will be able to inject any javascript code they want into the client. However, if you can guarantee that they're numbers then this won't be a problem.

Last but not least... you've defined alertNumber. Have you actually used this function?