PHP vs JavaScript For Dynamic HTML Pages PHP vs JavaScript For Dynamic HTML Pages javascript javascript

PHP vs JavaScript For Dynamic HTML Pages


Doing it client side means:

  1. Doing it in lots of different environments instead of a single one
  2. Having it break whenever a user comes along without JS (for whatever reason)
  3. Having it fail to work for the vast majority of bots (including search engines)
  4. Investing development time in converting all your logic
  5. Requiring the browser to make additional requests to the server, slowing down load times

When deciding if you should do something client side instead of server side, as a rule of thumb ask yourself two questions:

  1. Would the user benefit from getting instant feedback in response to them doing something? e.g. An error message for improper data in a form they are trying to submit. If so, then doing it client side would be beneficial.
  2. Can it be done server side? If so, do it server side first as it is more reliable (and for non-cosmetic things, harder to interfere with). Build on things that work.


It's not an either one or the other type of situation; generally you will need to do both.

Doing it client side will probably be slower, as the server still needs to figure out all the data but the client needs to render it; this will involve multiple requests (most likely) and DOM manipulation is slow (especially on older browsers).


Best practice would be to produce any necessary markup on the server side. Reasons for this include:

SEO: Most crawler bots won't parse Javascript, so they'll skip over anything crucial that you're generating with addElement.

Accessibility: Your site should basically functional without Javascript. Consider people who might be browsing your site on Kindles, older Blackberries, Nokias or other feature phones with data. They don't need all the fancy styles and effects, but they should at least be able to get around your site.

Consistency: JS can add yet another level of cross-browser variability. Why rely on client-side rendering of necessary markup? Do it server-side.

Of course, this advice can be taken in stride if you're developing an all-JS desktop app or using something like the Sencha Touch framework.