4 ms·
this seems fantastic if(condition) document.write('<!--') How else accomplish that?
by theendisney 4y ago
this seems fantastic
if(condition) document.write('<!--')
How else accomplish that?
- zffr 4y agoYou can use JS to set `display: none` on the relevant HTML elements
- johannes1234321 4y agoThe elements then still appear in the DOM and may be affected by DOM manipulation etc.
- arcanemachiner 4y agoHow about slapping the outerHTML into a <template> tag? Disclaimer: I didn't read the article and am just bikeshedding.
- lifthrasiir 4y agoSo do comments: you can get them via `Node.childNodes` (but not via `Element.children` which only returns elements).
- xg15 4y agoEvaluate the condition server-side and don't transmit the HTML in the first place.
- remram 4y agoIs that faster though? Can your Ruby/PHP/Python running on the server really do the conditional rendering faster than the JavaScript running in the browser?
- notRobot 4y agoMy experience is mostly with PHP, but the answer to your question is almost always yes.
- bastawhiz 4y agoYour server is already running a scripting language. Putting a script tag on the page necessarily means the browser then needs to parse and interpret some JavaScript, which it simply wouldn't otherwise need to do. If we assume the server and the client both execute an if statement at the same speed, yes, it's purely adding overhead to run that on the client (not to mention the potential for the additional cost of transmitting the eliminated code).
- theendisney 4y agoSorry, I thought it was obvious. The overhead is practically zero compared to the amount of html you can comment out. You can have a cached static html document with really huge (conditional) comments with tiny performance loss. display:none for example still does all kinds of things. Inserting html with js is even worse.
- Dalewyn 4y ago[flagged]