调用用户定义的 JavaScript 函数在 ejs 文件内。

huangapple go评论78阅读模式
英文:

Calling user defined JavaScript function inside ejs file

问题

I wrote my own JavaScript function inside an ejs file which I want to use it inside that file. The problem is that it is not working because the function is declared to be undefined.
I have declared that function inside the ejs file as illustrated below.

//my ejs file
<script>
        function c_tab (){
            //codes...
            return "data";
         }
        
    </script>
</head>
<body>
    <h1>WELCOME  TO  E J S </h1>
     <p>Ok this willrecievedata from server ... <%=  c_tab(); %>
    </p>

The error report says that c_tab is not defined

So how to use a user-defined JavaScript function inside an ejs file.

英文:

I wrote my own JavaScript function inside an ejs file which I want to use it inside that file. The problem is that it is not working because the function is declared to be undefined .
I have declared that function inside the ejs file as illustrated below.

//my ejs file
&lt;script&gt; 
        function c_tab (){
            //codes...
            return &quot;data&quot;;
         }
        
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;WELCOME  TO  E J S &lt;/h1&gt;
     &lt;p&gt;Ok this willrecievedata from server ... &lt;%=  c_tab(); %&gt; 
    &lt;/p&gt;

The error report says that c_tab is not defined

So how to use a user defined javascript function inside ejs file .

答案1

得分: 1

以下是翻译好的部分:

好的,对于你的问题,基本答案可能是:

&lt;%
    var foo = function(){return 'bar'};
%&gt;
&lt;body&gt;
    &lt;h1&gt;欢迎来到 EJS &lt;/h1&gt;
    &lt;p&gt;好的这将从服务器接收数据... &lt;%=  foo(); %&gt; 
    &lt;/p&gt;
&lt;/body&gt;

但是,由于你在代码中提到了“从服务器接收数据”,我假设你是通过 .render() 从 Express 传递本地变量到 EJS。

在这种情况下,这些变量应该只是“可用的”,但你想要它们在客户端代码中可用。尝试类似以下的方法:

&lt;script&gt;
  var foo = &lt;%= JSON.stringify(bar_obj_from_server)%&gt;
  var fooString = '&#39;&lt;%= bar_str_from_server %&gt;&#39;';
&lt;/script&gt;
英文:

Well, the basic answer to your question is probably

&lt;%
    var foo = function(){return &#39;bar&#39;};
%&gt;
&lt;body&gt;
    &lt;h1&gt;WELCOME  TO  E J S &lt;/h1&gt;
     &lt;p&gt;Ok this will recieve data from server ... &lt;%=  foo(); %&gt; 
    &lt;/p&gt;
&lt;/body&gt;

but since you said "receive data from the server" in your code, I presume you are passing local variables from express to ejs via .render()

In that case the variables should just be available, but you want them available in your client side code. Try something like

&lt;script&gt;
  var foo = &lt;%= JSON.stringify(bar_obj_from_server)%&gt;
  var fooString = &#39;&lt;%= bar_str_from_server %&gt;&#39;
&lt;script&gt;

答案2

得分: 0

使用Scriptlet标签而不是script标签:

<%
    function c_tab (){
        //codes...
        return "data";
    }
%>
英文:

Use Scriptlet tag instead of script tag:

&lt;% 
        function c_tab (){
            //codes...
            return &quot;data&quot;;
        }
%&gt;

huangapple
  • 本文由 发表于 2020年1月3日 21:21:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定