Let say that I have many Javascript inside pages. At this moment is pretty easy to initialize variable by simply using some Print/Echo statement to initialize JavaScript value.
Example: var x = <?php echo('This is a value');?>
I first thought that I could pass all variables value by parameter of function BUT it’s impossible because we have a lot of values (we have a multilanguage website and all text are from the server (BD)).
Example : initializeValues(<?php echo('Value1,Value2,Value3,Value...');?>);//JS Method that can be external of the page
More problem come when we want to take off all JavaScript from pages to move everything on external JavaScript file. What would be the good way to initialize all those variables? If I bind the JavaScript methods by using OnLoad of the document I won’t be able to use Print/Echo method to populate all values.
Any good pattern to resolve this task?
A very popular pattern is the use of the JSON format. There are libraries to produce it, and Javascript directly consumes it.