简体   繁体   中英

how can i use script function in ejs?

I want to use a script function in ejs.

I thought it would be simple, but no matter how I searched, I couldn't find any information.

test.ejs

<body>
<h1><% sum_ab(1,2) %></h1> // I want 3 to come out here.
<script>
function sum_ab(a,b) {
 var sum = a+b;
 return sum;
}
</script>
<body>

error message: sum_ab is not defined

I want to know how to use script function in ejs file.

Unless I'm mistaken that you cannot separate area (//this is js code) for write javascript codes. You can use js code within the HTML codes. I prepared an example code following according to your code:

            </div>
        <% function sum_ab(a,b) {
            var sum = a+b;
            return sum;
            }%>
        <h1><%= sum_ab(3,6) %></h1>
    </div>

Please try this code in the.ejs file. It'll be work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM