简体   繁体   中英

Javascript accessing ejs variable in Node.js context

I am trying my javascript code inside an HTML file to access an ejs variable.

This is the Node.js code:

res.render('target.ejs', {data:user});

When i do this in HTML:

<p> <h1><%= data %></h1> </p>

evertything is ok.

But when I try to access the variable inside a <script> , like this:

<script>
    var name = <%= data %>;
    alert(name);
</script>

then nothing happens. I also tried with a console.log(name); and still it didn't work.

Seems like you need some quotes.

Try

  var name = '<%= data %>';

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