0

I am passing parameter to HTML page with NodeJs router using:

res.render('dashboard', {ph_user: fname});

I can use this parameter in my HTML code with:

 <div id="greeting">Welcome:  <%= ph_user %></div>

However, i also would like to use this parameters with script on HTML page. I am sure I can extract if from element by ID, but that doesn't seem like the most efficient way. What is the best way, to access/use parameter in script on HTML page.

Any guidance is greatly appreciated.

1
  • You can put your EJS in a script tag with the variable Commented Jun 14, 2019 at 13:55

1 Answer 1

1

You can pass the variables like so:

<script type="text/javascript">
  var ph_user = <%- ph_user %>;
  // or window.ph_user = <%- ph_user %>
</script>

Be sure you understand the differences and risks between escaped and un-escaped variables. You can put this script above your other scripts and then it will be available to the other scripts.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for! Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.