1

I pass a variable title to my html document and access it there like this:

<h2><%= title %></h2> 

How can I access the same variable inside the <script> tag? I tried the following but it gives me an error:

<script>
    console.log(title);
</script>

Uncaught ReferenceError: title is not defined

Is this because the variable is in a different scope?

EDIT: I wanted to keep things simple in this post but based on the answers I realize that I should have mentioned that I use an external file for my script:

<script src="/javascripts/script.js"></script>

In this script I use jQuery for some stuff and I need the variable titlethere.

awaelchli
  • 796
  • 7
  • 23

4 Answers4

3

You can't access template variables in rendered output due to the nature of templates. However, you could render a javascript variable of the same name that is set to the literal value of the template variable. For example:

<script>
  var title = <%- JSON.stringify(title) %>;
  console.log(title);
</script>
mscdex
  • 104,356
  • 15
  • 192
  • 153
  • Thanks, but it is not quite what I need. I thought stuff inside the tag would be evaluated the same way as if I did it in an external file. Your solution does not work in my external file: Uncaught SyntaxError: Unexpected token – awaelchli Mar 29 '16 at 12:33
  • Why can't you place the suggested template partial *before* your ``? Then your `/javascripts/script.js` should have access to `title` just fine due to (global) scoping (assuming that you're rendering the output that contains `` and it's not from a static file being served). – mscdex Mar 29 '16 at 12:47
  • I tried this, but it does not compile there either. – awaelchli Mar 29 '16 at 12:48
  • What does "it does not compile" mean? – mscdex Mar 29 '16 at 12:49
  • My editor says expression expected and if I run it anyways it says in the console: Uncaught SyntaxError: Unexpected token – awaelchli Mar 29 '16 at 12:50
  • Then it sounds like you're not rendering that output, you're serving it up as a static file. You would need to call `res.render('htmlWithTitleVariable', { title: 'foo' })` whenever a client requests that html. – mscdex Mar 29 '16 at 12:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107639/discussion-between-aedug-and-mscdex). – awaelchli Mar 29 '16 at 12:58
0

You'd have to do something like this as shown is this answer.

<script>
  var title = <%- JSON.stringify(title) %>;
</script>
Community
  • 1
  • 1
Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
  • This does not compile for me :( – awaelchli Mar 29 '16 at 12:38
  • Read the accepted [answer here](http://stackoverflow.com/questions/16098397/pass-variables-to-javascript-in-expressjs). Depending upon how you've configued express/ejs you may need to approach it differently. – Carol Skelly Mar 29 '16 at 12:48
0

How can I access the same variable inside the <script> tag?

You can't. Variables in a program running on the server cannot be access by a program running on the client.

Since you have copied the value of the variable into the DOM of the page, you can read the value it held from there.

console.log(document.querySelector('title').firstChild.data);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

I defined it like this -

<body>
<h2 style = "visibility:hidden" id = "events"><%= events %></h2> 

<script>

function initMap (){

var list =document.getElementById('events').innerHTML ;
      console.log("list = ", list)
}
</script>