3

Short version: A variable is stored in a session variable req.session.userName and also in the json sent with res.render('/', {username: value}). How can I access either of those variables from inside client side javascript?(inside the script tags in the html)


MORE INFO: In my express&node webapp, a user has logged in from a separate login page. I have stored the username in a express-session variable: req.session.userName. I have redirected the page back to the main page which will call this:

router.get('/', function(req, res) {
 res.render('index', { title: 'Title of webpage', user: req.session.userName});
});

I would now like to alter parts of the html page based on the contents of the variable 'user' such as changing the sign in button to sign out and writing a welcome message to the user.

I understand that html can make use of that variable like so:

<%- user %>

However i need access to it in the client side script of my main page(html). Can JQuery make use of that json variable or session variable somehow like the following?(e.g. ask it if its undefined? use the contents of it to manip html elements?)

<script>
 $(function(){
 $("#welcome").html("Welcome, " + **user**);
 if (**user** == undefined){
 //do something
 }
 });
</script>
asked Nov 20, 2014 at 22:08

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.