Differences between revisions 5 and 6
Revision 5 as of 2009-07-16 17:30:43
Size: 823
Editor: CarlNobile
Comment:
Revision 6 as of 2009-07-17 20:36:36
Size: 1341
Editor: CarlNobile
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
When you cannot use a !JavaScript toolkit like [[http://jquery.com|jQuery]] these functions may come in handy. However, they do not work in IE.
When you cannot use a !JavaScript toolkit like [[http://jquery.com|jQuery]] these functions may come in handy. However, they do not globally work on IE. With IE you will need to supply the object that you want it set on.
Line 5: Line 6:
Node.prototype.insertAfter = function(newNode, refNode) { var insertAfter = function(newNode, refNode) {
Line 17: Line 18:
Node.prototype.getElementByClass = function(className, count) { var getElementByClass = function(className, count) {
Line 35: Line 36:

var extendNode = function() {
  // Most browsers have a Node object.
  if(window.Node) {
    Node.prototype.insertAfter = insertAfter;
    Node.prototype.getElementByClass = getElementByClass;
  } else { // IE doesn't, so we have to set it to the object we need it on.
    var container = document.getElementById("The Object You Need It Set On");
    container.insertAfter = insertAfter;
    container.getElementByClass = getElementByClass;
  }
}

JavaScript Helper Functions

When you cannot use a JavaScript toolkit like jQuery these functions may come in handy. However, they do not globally work on IE. With IE you will need to supply the object that you want it set on.

var insertAfter = function(newNode, refNode) {
  var result = null;

  if(refNode.nextSibling) {
    result = this.insertBefore(newNode, refNode.nextSibling);
  } else {
    result = this.appendChild(newNode);
  }

  return result;
}

var getElementByClass = function(className, count) {
  var result = null;

  for(var i = 0; i < this.childNodes.length; i++) {
    var classCount = 0;

    if(this.childNodes[i].className == className) {
      if(classCount == count) {
        result = this.childNodes[i];
        break;
      }

      classCount++
    }
  }

  return result;
}

var extendNode = function() {
  // Most browsers have a Node object.
  if(window.Node) {
    Node.prototype.insertAfter = insertAfter;
    Node.prototype.getElementByClass = getElementByClass;
  } else {  // IE doesn't, so we have to set it to the object we need it on.
    var container = document.getElementById("The Object You Need It Set On");
    container.insertAfter = insertAfter;
    container.getElementByClass = getElementByClass;
  }
}

JavaScriptHelperFunctions (last edited 2009-07-17 20:58:15 by CarlNobile)