Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the fastest and/or cleanest way to store text content in javascript/DOM? [closed]

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>
like image 849
Ryan Detzel Avatar asked Dec 27 '12 13:12

Ryan Detzel


1 Answers

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.

like image 72
Oleg V. Volkov Avatar answered Nov 07 '22 18:11

Oleg V. Volkov