2010年5月9日星期日

How to write a jQuery Plugin

Everyone can design their own jQuery plugins.  Once you understand jQuery, you no longer write pure Javascript for DOM; once you know how to write jQuery plugin, you no longer write repeative jQuery code.  By using jQuery Plugin, you can focus mainly on the business logic of a webpage.  That's the reason why I keep writing a lot of jQuery plugins.  

Writing jQuery plugins is easy.  Everyone who has the basic jQuery knowledge is capable to write jQuery plugins.  It is just as simply as appending  a function to jQuery.fn object.

However, if you only have little knowledge about jQuery Plugin,  you can even try to modify the content from a jQuery Plugin Template.

By the way, paying attention to the  following designing rules should help improving the quality of a jQuery Plugin:


  1. Avoid using global variables. jQuery plugins should be worked properly in all kinds of circumstances and global variables should be avoided in order to prevent conflict of usages of variables with same name.
  2. Uses the "jQuery" variable instead of "$" to avoid conflict of libraries. There may be conflict of libraries when some libraries are using "$", too.
  3. You plugin should return the same DOM list as they are passed from jQuery selector engine if possible. To return the same DOM list can ensure the chainability in using the plugin.

Happy Programming,

Cloudgen Wong

0 意見:

張貼意見