简体   繁体   English

如果带脚本标签的EJS中有条件的条件不起作用

[英]IF conditional in EJS with Script tag doesn't work

I've some problem in here. 我在这里有些问题。 I put script tag inside IF Conditional. 我将script标记放在IF条件中。 Nah, script tag is running when condition is TRUE or FALSE. 不,条件为TRUE或FALSE时, script标记正在运行。

For example 例如

<% if (info) { %>
    <p><%=info%></p>
            <script type="text/javascript">
            alert("Hi")
            </script>
    <% } %>

Result : 结果:

IF = TRUE IF = TRUE

alert("Hello world")
<p>Hello world</p>

IF = FALSE 如果=假

alert("Hello world")

Paragraph attributes is rendering when IF conditional is TRUE. 如果IF条件为TRUE,则呈现段落属性 But script tag is running when IF Conditional is TRUE or FALSE, what i want the script tag running like HTML attributes. script标签运行时,如果有条件的话是真是假,我想做的script代码运行像HTML属性。

Why this is happened? 为什么会这样呢? Thanks 谢谢

Okay, i answer my own questions. 好吧,我回答我自己的问题。 This is solved by added <% if (info.length > 0) { %> 通过添加<% if (info.length > 0) { %>

Since my route file like this 由于我的路线文件是这样的

function isAuth(req, res, next){
    var token = req.session.token;
    if(token){
        jwt.verify(token, 'jwtsecret', function(err, decoded){
            if(err){
                req.flash('info', err)
                res.info = req.flash()
                res.redirect('/login')
                return;
            }
            req.decoded = decoded;
            req.session.username = decoded.username
            next();
        })
    }
    else{
        req.flash('info', "Please login first")
        res.redirect('/login')
    }
};

Router.get('/login', function(req, res){
    console.log(req.flash)
    res.render('../views/login', {info : req.flash('info')})
})
<% if (info.length > 0) { %>
  <script type="text/javascript">
    alert("Hello world")
  </script>
  <% } %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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