Saturday 7 January 2012

Unobtrusive JavaScript



Unobtrusive JavaScript is the practice of separating out any JavaScript behavior
code from your content structure and presentation.

<input type="button" onclick="validateDate()" id="_btn" />

The above line of  html content of a  button is some ways coupled to the behaviour of onclick.This can be decoupled by using Unobtrusive JavaScript paradigm.

Little modification to the above will set the things right :)

<input type="button"  id="_btn" />.

Instead of embedding the behavior in the markup,this can be segregated by moving the script to script block with in <head> tag else to the bottom of your document considering some performance stuff.

<script>
window.onload=function(){
document.getElementById"(_btn").onclick=function(){
//do the stuff here 
}
}
</script> 



The above lines of code is similar to $(document).ready (function()......) in jquery.This helps in clear seperation of behavior from structure.

Thanks for reading....cheers... :-) :-) :-)


No comments:

Post a Comment