3

I have html snippets and i was wondering where I should keep them... For example:

Currently I have the following in my HTML page

<div id="Mailbox" class="ui-corner-all">
    <div id="Messages">
        <ul class="messageList">
            <li class="noMessage">You have no messages</li>
        </ul>
    </div>
    <div class="trashIcon ui-corner-bottom">
        Delete Selected Message(s)
    </div>
</div>

Then, when the page loads, jquery and javascript turns this into a full functioning mailbox system.

What I am wondering is - Should I bother with the HTML snippet, or just build the entire thing through JS?

Zholen
  • 1,762
  • 2
  • 24
  • 53

2 Answers2

4

Having the markup in your page potentially degrades much better for users with no script. Plus, it's usually easier to work with existing markup than build it completely from scratch with JavaScript.

Sometimes templating can be an appropriate solution. This will not degrade for users without script, but if you have a script-heavy page that needs to create a lot of markup dynamically, templates can make this simpler.

If you find that you are duplicating blocks of markup from page to page, this should probably be addressed server-side by storing the common content in a single location.

Community
  • 1
  • 1
Tim M.
  • 53,671
  • 14
  • 120
  • 163
0

Depends if you're writing an application or a website. Does SEO matter? Does accessibility matter? Do you need to support a wide range of browsers and devices?

If not then I'd write it in JS but take a look at some MVC frameworks such as backbone.js or JavaScriptMVC etc. in which case you'll likely want to be using client-side templating so you're html should be in the page but not in the DOM.

Richard Scarrott
  • 6,638
  • 1
  • 35
  • 46