What for should i use JavaScript in simple www pages?!

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

JavaScript has a wide range of uses, which may overlap with CSS. Many learners are tempted to use JS in place of CSS. This is never a good idea. CSS should be used to its fullest extent, and not script when dealing with visualization.

JS is about behaviors that cannot be replicated using CSS. For instance, CSS cannot validate a web form. For that we need script that can target a form control and examine the user input for that control. It can manipulate the DOM to display error messages to the user and disable the SUBMIT button until such repairs are made.

We can use JS to suppress and display any part of a web page in response to a users interaction. That is why we say that JS adds interactivity to an otherwise static document, keeping in mind that CSS offers some of the same, with limitations that need to be learned.

JavaScript can also communicate with a server using XHR, i.e., XML over HTTP Request. This allows the developer to validate form data on the server, and respond with some document or HTML snippet.

Today’s web is anything but static. A web page without interactivity is rather a rare thing, these days. JS helps to gather information about the user agent and tailor responses to that UI. It can help to make decisions about which response to give to which user inputs or interaction.

Script can be used to write and modify a style sheet, write and modify page text, write and modify cookies, write and obfuscate email addresses so spam bots can’t find them, but the user can. We haven’t really put more than scratch up to this point of the complete range of script possibilities. The bottom line is scripting has and always will be how we give our pages and apps their horse-power and versatility.

2 Likes

Adding on to what @mtf said, HTML and CSS are really instructions on how to display information, so if that’s all you need to do, then you don’t really need JavaScript. For example, a teacher posting homework assignments just needs do display information.

JavaScript is for when you want to collect or change information based on user input. Some examples are collecting user input on a form, or getting information from another source (like having the user enter a search term, then executing the search).

1 Like