5 ms·
Why are we creating HTML from within Javascript? It would be much better to use HTML for that and simply toggle the display/visibility of DOM elements via CSS
by rocky1138 10y ago
Why are we creating HTML from within Javascript?
It would be much better to use HTML for that and simply toggle the display/visibility of DOM elements via CSS or JS.
- mattmurdog 10y agoWannabe React. Engineers who over engineer.
- emn13 10y agoThe alternative being templating which is several orders of magnitude more complex. Fortunately, it saves you a few characters here and there.
- BinaryIdiot 10y agoAbsolutely this! Instead of awkwardly building DOM in JavaScript just build it in HTML, manipulate in JavaScript and style in CSS. This separation still works amazingly even if popular frameworks like React are blurring the lines.
- deleted 10y ago[deleted]
- nathancahill 10y agoHTML is dead. Long live HTML!
- hmottestad 10y agoWe are creating HTML from within Javascript because: - We want to generate HTML from our data (eg. database) - We want an interactive experience (eg. with toggling visibility) You could argue that we should generate the HTML on the backend, but then you are still generating HTML, and now you have two systems that interact with the HTML instead of one.
- flukus 10y agoWhy not use one of the dozens of templating libraries?
- emn13 10y agoBecause templating languages are without exception bug-breeding grounds that no human being can actually use without producing errors, which the browser then sometimes proceeds to auto-correct (html being html), which leads to a kind of "approximate" programming style, aka nirvana for security holes and other bugs. Oh, and composition is terrible pain too, encouraging over-complex UI components that do it all. And learning whatever flavor cleverly-insufficient control-flow and looping constructs this templating library uses is a pointless waste of time (and source of bugs) too. Why in the heavens would you ever choose to use a templating library and blur the lines between content (user input) and UI structure? Why reinvent the control-flow wheel? Templating represents programmers realizing that this newfangled browser thing can already parse HTML, and hey, can't we do something clever with that - sure, if we unnecessarily serialize to a string first, yes! No thank you.
- deleted 10y ago[deleted]
- akst 10y agoI straight up prefer templating for ergonomic reasons, especially when I see someone inlining higher order functions & JS control flow structures in a HTML DSL. But I can sympathise with where you're coming from, as I have not so fond memories of backbone + [insert templating library]. A lot the problems can be addressed through better tools & a pre compilation stage where the templates are transformed into JS & data input are escaped for security reasons. One example of this is Ember.js's HTMLbars which address basically all of what you just listed there. edit: That said if for whatever reason I couldn't use Ember, I'd probably choose some flavour of JSX over most other templating libraries, lol
- rimantas 10y agoToggling visibility (and other stuff) is best done by changing classes on the element.