Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access server-side javascript variable for manipulation with client side javascript

Using sails.js as a framework for a node.js app I'm building.

I'm trying to retrieve information previously set by a user (i.e. settings for my app) from the back end, and then use these settings in my client-side javascript.

I currently have a controller which retrieves the information from the database and passes it to the view where it will be needed.

return res.view('dashboard', { configs: configurations });

I know it is accessible when rendering html, but don't know how to pass it to the javascript environment running on the browser.

Any tips would be amazing!

To clarify, I'm not trying to render this information in the html page (i.e. the method of using <%= variable %>, I want to use it in the javascript running on the page.

like image 734
nbdy_ Avatar asked Oct 19 '22 13:10

nbdy_


1 Answers

If i understood your question correctly; you can use the server side variable in your script tag.

<script>

    var something = <%= serversideVariable %>;

    var  <%= serversideVariable %> = "some value";

</script>

This would work as javascript.

like image 200
serkan Avatar answered Oct 22 '22 04:10

serkan