I read a piece of Javascript code, where the programmer added something similar to this:
html = "
<div class='hidden-context-menu'>
<ul><li><a class='select-all'>All</a></li>
<li><a class='select-none'>None</a></li>
...
...
...
</ul>
</div>
"
and then, in some other parts of the webapp, this piece of code is used and rendered in the page.
output_html(html);
I assume, considering this specific example of a hidden context menu, that maybe the developer did’t want to repeat himself..
Is this kind of practice encouraged or it has to be considered a wrong approach?
I would say that relying on Javascript to generate your HTML is probably a bad idea. What if the user has Javascript turned off in their browser?
I think that it is more common to find HTML generated by a server-side script, like PHP. The resulting page that the client receives is the same as it would be if PHP wasn’t used, and the developer just typed out the HTML repeatedly.