Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Unobtrusive Javascript in layman terms? [closed]

What is Unobtrusive Javascript in layman terms? An example would be nice to aid my understanding.

like image 706
Imran Avatar asked Dec 18 '10 16:12

Imran


People also ask

What is the meaning of unobtrusive JavaScript?

In short, unobtrusive JavaScript is a way of writing JavaScript so that your site visitors are not shut out of your site for one of these reasons—even if your JavaScript is not working correctly for them, they should still be able to use your site, albeit at a more basic level.

What is the meaning of unobtrusive JavaScript explain us by any practical example?

Unobtrusive JavaScript is a general approach to the use of client-side JavaScript in web pages so that if JavaScript features are partially or fully absent in a user's web browser, then the user notices as little as possible any lack of the web page's JavaScript functionality.

What is the meaning of unobtrusive JavaScript in MVC?

Unobtrusive JavaScript is a general term that conveys a general set of guidelines or margins to the term REST. REST is nothing but the Representational State Transfer. We can explain Unobtrusive JavaScript as- it is not your particular JavaScript code that you generally use in your markup page.

What is the practical application of unobtrusive JavaScript?

Introduction. Unobtrusive JavaScript is the way of writing JavaScript language in which we properly separate Document Content and Script Content thus allowing us to make a clear distinction between them. This approach is useful in so many ways as it makes our code less error prone, easy to update and to debug.


1 Answers

Checkout the wikipedia article:

  • Unobtrusive JavaScript

"Unobtrusive JavaScript" is a general approach to the use of JavaScript in web pages. Though the term is not formally defined, its basic principles are generally understood to include:

  • Separation of functionality (the "behavior layer") from a Web page's structure/content and presentation
  • Best practices to avoid the problems of traditional JavaScript programming (such as browser inconsistencies and lack of scalability)
  • Progressive enhancement to support user agents that may not support advanced JavaScript functionality[2]

So it is basically separating behavior or javascript from presentation or html.

Example:

<input type="button" id="btn" onclick="alert('Test')" /> 

That is not unobstrusive javascript because behaviour and presentation are mixed. The onclick shouldn't be there in html and should be part of javascript itself not html.

With above example, you can go unobstrusive like this:

<input type="button" id="btn" /> 

JavaScript:

var el = document.getElementById('btn'); el.onclick = function(){   alert('Test'); }; 

That time we have separated javascript from html with a very basic example.

Note:

There is more to unobstrusive javascript as can be checked out on wikipedia article.

like image 177
Sarfraz Avatar answered Sep 30 '22 20:09

Sarfraz