Izzo
Izzo

Reputation: 4928

Should Javascript be used to modify HTML?

I just recently started learning javascript and have a question regarding the'proper use'. I'm still trying to identify the role of Javascript within a website, and I'm curious whether or not it would be considered ok to have Javascript modified the HTML of a web page.

Let's say I have a panel on a web page. This panel houses a list. I would like users to be prompted to add items to this list.

I was thinking that it would be possible to use Javascript to generate list items to add to the list. However, this would be modifying the actual number of HTML elements on the web page... For some reason, this just seems 'hacky'. When I think of HTML, I think of a static structure that should come to life with CSS and Javascript.

So my question: is it considered okay to have Javascript modify the HTML of a web page? What about the case of adding items to a list?

Thank you!

Upvotes: 0

Views: 426

Answers (4)

Alex Kudryashev
Alex Kudryashev

Reputation: 9460

Short disclamer: Javascript may modify DOM content in a browser but never change original HTML served from Web server.
Modern Web is unthinkable without javascript. JS allows to make static HTML interactive and improve User Experience (UX). As any sharp tool JS can produce a masterpiece out of nearly dead page and cut throat to a blooming static content.
Everything is up to the developer.

When not to use JS

Do not use JS to deliver ever-green content to the page. Web bots (crawlers) don't run JS, and you message "I come to this world to testify to the truth" may appear "a voice of crying out of desert" and be non-indexed and thus unread.

When JS in the must

Every time your page visitor does something the page should respond with proper action (using JS or, if possible, just CSS).
This is especially important when a prospect fills in a form. To err is human so a developer via JS should help the visitor to make wrong things right. In many cases it is possible without requesting server and in even more cases the answer should come from the server. And JS is your best friend in this case.
Javascript never lives alone. Browser object is its trustful ally. Most modern browsers support XMLHttpObject A.K.A AJAX (you may remember this name from ancient Greek history) which communicates with the server without reloading the page.
The idea of AJAX which stands for Asynchronous Javascript And Xml is to send request and receive response from the server asynchronously without blocking page in the browser.
Native javascript may be difficult to understand to many beginner developers. To make thing easier there are a lot of JS libraries with jQuery being most known.

Returning to the OP's question, Should Javascript be used to modify HTML?
The answer is: It Depends.

Upvotes: 0

Jonathan
Jonathan

Reputation: 11494

What matters is the user's experience of the (HTML) document. The representation of "the document" can change by utilising a language like javascript that "manipulates the DOM" - and the DOM is like an instance of the HTML document, or "document session" if you will.

So in a way, no, the HTML is touched. It is positively manhandled by javascript - indirectly and in a non-persistent way. But if you want to be pedantic... we say it isn't and leave half the readers confused.

When to use javascript and when not to. It's swings and roundabouts. You need to learn (mostly from experience) when one particular tool suits the situation. It usually boils down to some core considerations:

  1. HTML is for markup. Structure. Meaning.
  2. CSS is for style, feel, appearance.
  3. Javascript is for those situations where none of the above work.

And I'm neglecting to mention server-side processing with the obvious disclaimer that all processing that ought to be done in privacy is done on the server (with a programming language like PHP or Ruby for example).

Sometimes you get the grey area in-between where you can do something either way. Those situations you may ask yourself a question like... would it be processed quicker if the client (user's computer) processes it, or the server... and that's where experience comes in.

Upvotes: 1

Charlie
Charlie

Reputation: 23778

It depends on the case to decide if you should manipulate DOM directly or let the JS do it.

  • If you have a static html page, just do your html and hand craft the DOM. There is no need for JS to get a hand here.

  • If you have a semi static html page where the user actions change part of it - then get the JS to do the changing part.

  • If you have a highly dynamic html page (like single page app) - you should get the JS to render html mostly.

Using plain JS however is not the best for you in this age of great JS evolution. Learn it -but also learn to use good libraries and frameworks which can take you to the track very fast.

I recommend you to learn Jquery and Angular 2 which make you feel like learning a super set of JS straightaway.

Upvotes: 0

Greg Borbonus
Greg Borbonus

Reputation: 1384

Javascript is a programming language designed so it can modify the document that is being displayed(the DOM), the actual HTML is never touched.

Javascript has a place on a website and modifying the document/dom is perfectly acceptable and without it, would make javascript almost useless. CSS is great for certain tasks, but you can't do everything in CSS, though CSS5 is coming pretty close for many tasks.

Rewriting the entire DOM IS bad practice, but using it to shift an element's position based on an action, or creating a popup overlay is perfectly acceptable.

Remember the gold rule: Modify as little as possible to accomplish the goal.

Upvotes: 1

Related Questions