Wednesday, 18 July 2012

Common utility methods of JQuery


jQuery provides several inbuilt utility methods. These utility methods uses $.namespace and they are very useful and handy for your routine programming tasks. I have put together some common jQuery Utility methods to make your programming routine easy and can save your valuable time.
  1. $.isArray(Object): Returns true if the object is an array.
//Code Starts
var arrFirst = [50, 22, 10, 19, 22, 10];
var isArray = $.isArray(arrFirst);
//Code Ends

  1. $.isEmptyObject(Object): Returns true if the object doesn’t define any methods or properties.
//Code Starts
var arrFirst = [50, 22, 10, 19, 22, 10];
var isObject = $.isEmptyObject(arrFirst);
//In this case, isObject will be false.
//Code Ends

  1. $.isFunction(Object): Returns true if the object is a function.
//Code Starts
function myFunc() {
//function code
          }
var isFunction = $.isFunction(myFunc);
//Code Ends

  1. $.isNumeric(Object): Returns true if the object is a number.
//Code Starts
var num = 1;
var isNumeric = $.isNumeric(num);
//Code Ends

  1. $.isWindow(Object): Returns true if the object is a Window. This method accepts an argument and determines whether the argument is a window. This is used in a number of places in jQuery to determine if we're operating against a browser window (such as the current window or an iframe).This returns true is object is window, otherwise false.
//Code Starts
$(document).ready(function(){
alert($.isWindow(window));
var isFrame = $("#objFrm");
alert($.isWindow(isFrame));
});
//Code Ends
Here objFrm is defined as an iframe.

  1. $.isXMLDoc(Object): Returns true if the object is an XML document. Check to see if a DOM node is within an XML document (or is an XML document).
//Code Starts
$(document).ready(function(){
$.isXMLDoc(document) // false
$.isXMLDoc(document.body) // false
});
//Code Ends

  1. $.type(Object): Returns the built-in JavaScript type for the object.
//Code Starts
$(document).ready(function(){
          var iVal = 12;
          $.type(iVal); //number
          iVal = 'jQuery';
          $.type(iVal); //string
});
//Code Ends
Below is the list of types which can be returned by this method based on the value:
    • boolean
    • number
    • string
    • function
    • array
    • date
    • regexp
    • null
    • undefined
    • object

  1. $.trim(Object): This method removes white spaces from beginning and end of the string.
//Code Starts
$(document).ready(function(){
          var str = '     jquery by example       ';
          str = $.trim(str);
});
//Code Ends

No comments:

Post a Comment