I typically see version 1 but some open source projects I work on use version 2 and I've used version 3 in the past. Does anyone have a more elegant solution, maybe one that's more scalable?
Version 1:
var text1 = 'this is my script\'s content';
Version 2:
<script type="text/plain" id="text1">This is my content</script>
<script> var text1 = $('#text1').html(); </script>
Version 3:
<input type="hidden" id="text1" value="this is my content">
<script> var text1 = $('#text1').val(); </script>
Version 4:
<span class="hidden" id="text1">this is my content</span>
v1, because it is the ONLY way to store text in JavaScript. Everything else you've listed is storing text in DOM.
DOM is "presentation" and it is generally bad idea to mix "data" and "presentation". For example when you implement collision in 3D FPS, you will never scan models from screen (presentation), you'll compare their coordinates in their data instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With