Reputation: 10667
What is the best way to add dynamic content to JavaScript, especially configuration settings?
A couple possibilities are:
Upvotes: 2
Views: 5652
Reputation: 169411
We do not generate dynamic JavaScript.
We do generate dynamic HTML
Then use progressive enhancement to enhance the HTML with JavaScript. If you want to store data, store it in HTML5 data-
attributes on relevant HTML elements.
Alternatively you write a Web Service and query it with AJAX to get dynamic data.
Upvotes: 2
Reputation: 328624
I suggest to put as much JavaScript as possible in a static resource (so the browser can cache that) and just generate the smallest possible dynamic part. Often that's just a few variable assignments.
This approach saves network bandwidth, it makes the code generation more robust, and you can test the static JavaScript as usual.
Upvotes: 12