Access to server side javascript variable to manipulate javascript client side

Using sails.js as the framework for the node.js application that I am creating.

I am trying to extract information previously set by the user (e.g. settings for my application) from the back, and then use these parameters in my client side javascript.

I currently have a controller that retrieves information from the database and passes it to the view where it is needed.

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

I know that it is available when rendering html, but does not know how to pass it to the javascript environment running in the browser.

Any tips would be awesome!

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

+5
source share
2 answers

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

 <script> var something = <%= serversideVariable %>; var <%= serversideVariable %> = "some value"; </script> 

This will work as javascript.

+3
source

You can create something similar in your view. During page loading, this script will be executed, and any subsequent script will have access to GlobalSettings .

 <script type="text/javascript"> var GlobalSettings = { //.. JSON with parameters for client } </script> 
0
source

All Articles