1

Currently I am creating a website which is completely JS driven. I don't use any HTML pages at all (except index page). Every query returns JSON and then I generate HTML inside JavaScript and insert into the DOM. Are there any disadvantages of doing this instead of creating HTML file with layout structure, then loading this file into the DOM and changing elements with new data from JSON?

EDIT: All of my pages are loaded with AJAX calls. But I have a structure like this:

<nav></nav>
<div id="content"></div>
<footer></footer>

Basically, I never change nav or footer elements, they are only loaded once, when loading index.html file. Then on every page click I send an AJAX call to the server, it returns data in JSON and I generate HTML code with jQuery and insert like this $('#content').html(content);

Creating separate HTML files, and then for example using $('#someID').html(newContent) to change every element with JSON data, will use even more code and I will need 1 more request to server to load this file, so I thought I could just generate it in browser.

EDIT2: SEO is not very important, because my website requires logging in so I will create all meta tags in index.html file.

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Dove
  • 125
  • 1
  • 2
  • 8
  • 1
    To my understanding, javascript and DOM arent friends on this subject. In my expierence I find it easier to edit and create html in html-files. Im a fan of seperating my resources (js only for js, html-files for html, etc) – Martijn Oct 03 '13 at 12:07
  • why are you doing this? – Mike H. Oct 03 '13 at 12:09
  • Why are you asking that question @MikeHometchko? That's the way many single page apps work. – JayC Oct 03 '13 at 12:12
  • @JayC eh, not really. If he was to say "I generate HTML through Javascript/JQ, XAML, XML, handlers, views, razor/asp forms, etc etc etc" then I wouldn't be so suspicious. – Mike H. Oct 03 '13 at 12:16

4 Answers4

3

In general, it's a nice way of doing things. I assume that you're updating the page with AJAX each time (although you didn't say that).

There are some things to look out for. If you always have the same URL, then your users can't come back to the same page. And they can't send links to their friends. To deal with this, you can use history.pushState() to update the URL without reloading the page.

Also, if you're sending more than one request per page and you don't have an HTML structure waiting for them, you may get them back in a different order each time. It's not a problem, just something to be aware of.

Returning HTML from the AJAX is a bad idea. It means that when you want to change the layout of the page, you need to edit all of your files. If you're returning JSON, it's much easier to make changes in one place.

ColBeseder
  • 3,579
  • 3
  • 28
  • 45
  • I dont think the question is so much about wether the AJAX method is good or bad, but more about wether loading an htmlfile via ajax, or dom elements from the file. – Martijn Oct 03 '13 at 12:08
2

One thing that definitly matters :

How long will it take you to develop a new system that will send data as JSON + code the JS required to inject it as HTML into the page ?

How long will it take to just return HTML ? And how long if you can re-use some of your already existing server-side code ?

and check how much is the server side interrection of your pages...

also some advantages of creating pure HTML :

1) It's simple markup, and often just as compact or actually more compact than JSON.

2) It's less error prone cause all you're getting is markup, and no code.

3) It will be faster to program in most cases cause you won't have to write code separately for the client end.

4) The HTML is the content, the JavaScript is the behavior. You're mixing both for absolutely no compelling reason.

in javascript or nay other scripting language .. if you encountered a problem in between the rest of the code will not work

and also it is easier to debug in pure html pages

my opinion ... use scriptiong code wherever necessary .. rest of the code you can do in html ... it will save the triptime of going to server then fetch the data and then displaying it again.

Keep point No. 4 in your mind while coding.

Mayur Gupta
  • 762
  • 3
  • 8
  • 23
1

If you cared about SEO you'd want the HTML there at page load, which is closer to your second strategy than your first.

Update May 2014: Google claims to be getting better at executing Javascript: http://googlewebmastercentral.blogspot.com/2014/05/understanding-web-pages-better.html Still unclear what works and what does not.

Further updates probably belong here: Do Google or other search engines execute JavaScript?

Community
  • 1
  • 1
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
1

I think that you can consider 3 methods:

  • Sending only JSON to the client and rendering according to a template (i.e. handlerbar.js)

  • Creating the pages from the server-side, usually faster rendering also you can cache the page.

  • Or a mixture of this would be to generate partial views from the server and sending them to the client, for example it's like having a handlebar template on the client and applying the data from the JSON, but only having the same template on the server-side and rendering it on the server and sending it to the client in the final format, on the client you can just replace the partial views.

Also some things to think about determined by the use case of the applicaton, is that if you are targeting SEO you should consider ColBeseder advice, of if you are targeting mobile users, probably you would better go with the JSON only response, as this is a more lightweight response.

EDIT:

According to what you said you are creating a single page application, if this is correct, then probably you can go with either the JSON or a partial views like AngularJS has. But if your server-side logic is written to handle only JSON response, then probably you could better use a template engine on the client like handlerbar.js, underscore, or jquery templates, and you can define reusable portions of your HTML and apply to it the data from the JSON.

Robert Onodi
  • 120
  • 5