Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use a javascript framework library?

I'm writing a small web that just makes some animation and shows some information as a homepage and a list of links. All that is going to be generated dynamically in the client side. So everything is going to be javascript and XML.

Recently I've been reading some questions in SO about javascript, and most of the situations involved the use and/or recommendation of a framework (jquery and friends). When a small web development should start considering the use of such a framework?

I've been until now doing my stuff just with plain javascript, as far as I'm not implementing a big site is it worth the learning a framework?

Thanks

like image 273
alvatar Avatar asked May 14 '09 14:05

alvatar


People also ask

When should I use a JavaScript framework?

When you're building a big application, it could be smart to use a JavaScript framework. A framework, most of the time, comes with great support from a community. Most of the time, a community will provide a lot of learning material that helps to build an application for long-term support.

Why would you use a JavaScript library?

Some client-side JavaScript libraries help abstract away quirks of the web platform. A library can also serve as a learning tool. For example, if you're unfamiliar with animation easing functions, the source code of a library can teach you how such easings work.

Should I use a JavaScript library?

As a developer, having and using the right JavaScript libraries is important. It will make you more productive and will make development much easier and faster. In the end, it is up to you which library to prefer based on your needs.

What's the difference between a JavaScript framework and a JavaScript library?

The main distinction between a framework and a library is that a framework inverts program control. It informs the developer of what they require. A library, however, does not. Instead, a programmer calls the library when and where he needs it.


1 Answers

On SO you will find a lot of people (including me) who advocate the use of jQuery (in particular). To me, it's everything a framework should be: small, lightweight, extensible, compact yet powerful and brief syntax and it solves some pretty major problems. I would honestly have a hard time trying to envision a project where I wouldn't use it (or another framework).

The reason to use it is to solve browser compatibility issues. Consider my answer to javascript to get paragraph of selected text in web page:

function getSelectedParagraphText() {
  var userSelection;
  if (window.getSelection) {
      selection = window.getSelection();
  } else if (document.selection) {
      selection = document.selection.createRange();
  }
  var parent = selection.anchorNode;
  while (parent != null && parent.localName != "P") {
    parent = parent.parentNode;
  }
  if (parent == null) {
    return "";
  } else {
    return parent.innerText || parent.textContent;
  }
}

If you're familiar with Javascript a lot of this should be familiar to you: things like the check for innerText or textContent (Firefox 1.5) and so on. Pure Javascript is littered with things like this. Now consider the jQuery solution:

function getSelectedParagraphText() {
  var userSelection;
  if (window.getSelection) {
      selection = window.getSelection();
  } else if (document.selection) {
      selection = document.selection.createRange();
  }
  var parent = selection.anchorNode;
  var paras = $(parent).parents("p")
  return paras.length == 0 ? "" : paras.text();
}

Where jQuery really shines though is with AJAX. There JavaScript code snippets around to find the correct object to instantiate (XMLHttpRequest or equivalent) to do an AJAX request. jQuery takes care of all that for you.

All of this for under 20k for the core jQuery Javascript file. To me, it's a must-have.

like image 124
cletus Avatar answered Sep 21 '22 11:09

cletus