Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice - pre-write HTML in document with display:none, or create with JS?

I am developing a large scale HTML5 app, and I really wonder about this issue. I will have a lot of dialog boxes and tabs that will open by user interaction.

I wonder what is the best practice - writing all the dialog boxes and tabs in the HTML document with display:none to all of them, or create these HTML sections on the fly with JS or jQuery every time the user making the relevant interaction.

What is better in terms of performance, ease of development, readability, etc?

Any help will be appreciated.

like image 656
Light Avatar asked Apr 24 '13 06:04

Light


People also ask

Which of the following are good practices regarding positioning of script tags in your HTML file?

Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

What are the three methods for adding JavaScript code to HTML documents?

There are typically three ways to add JavaScript to a web page: Embedding the JavaScript code between a pair of <script> and </script> tag. Creating an external JavaScript file with the . js extension and then load it within the page through the src attribute of the <script> tag.

How can you add JavaScript to an HTML page?

You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.


1 Answers

I'll try to address this question as good as I can.

1 - As I said in the comments, Avoid inline styling. First and foremost this is because inline styling voilates DRY. Having to repeat the same thing over and over again for this is very bad for maintenance and for developing since instead of changing code once you have to change it at ~100 places.

2 - Avoiding inline styling is also good for accessibility, some screen readers and search engine crawlers do indexing work and reading work based on css selectors and thusly using inline styling will force them to either ignore or misintrepret things.

3 - When working as developers it's easy to do inline styling "just for the fun" but what you're actually doing is mixing concerns. HTML is the content and CSS is the design. Mixing these two usually leads to headaches and making my job as a developer that comes after you a pain in the effin ass since I have no idea what's styled and how.

Now, onto performance.

When you use inline styles, what you're telling the browser is basically "hey, for every page page view apply these styles to all of these elements." Now, this just became really apparent why this is bad. You have no ability to cache and store your css and basically forces the browser to rerender your styles every time. Using an external CSS file will actually help you speed up your site since the browser caches it.

That was that for the css part.

The javascript you had asked about.

As I said, hide things with css and show with javascript. Now why do you want to do this instead of pulling everything in? Well, you can do both. If you're only a webbrowser experience then you can do either, it doesn't matter. I myself prefer to have stuff in the DOM because it relates to content and if you're a large app having dozens of dozens of ajax calls will only make it harder for maintenance. I believe if you have to ajax stuff in make sure it counts and is logical and not just for the kicks (I think this applies if only you have jQuery and plain javascript at your disposal).

If you're working with backbone.js, for example, it's based on views and introduces some form of "MVC" into your frontend enabling you to have views with subviews that can pull content in from the server.

Hope that helps a bit with making a decision! :)

like image 132
Henrik Andersson Avatar answered Sep 20 '22 05:09

Henrik Andersson