Jump to content

User:Supadawg/util.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function createXMLHTTP( method, uri, callback, options )
{
  var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest()
              : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP")
                : null;
  if ( xmlhttp ) {
    xmlhttp.onreadystatechange = callback;
    xmlhttp.open( method, uri, true );

    if ( options && options.headers )
      for ( var key in options.headers )
        xmlhttp.setRequestHeader( key, options.headers[key] );

    var body = null;
    if ( options && options["body"] )
      body = options["body"];
    xmlhttp.send( body );
  }
  return xmlhttp;
}

function createNode( parent, type, attrs, options )
{
  var doc = (options && options.doc) ? options.doc : document;
  var node = doc.createElement( type );
  for ( var attr in attrs )
    node.setAttribute( attr, attrs[attr] );
  if ( parent )
    node = parent.appendChild( node );
  return node;
}

function insertAfter( newNode, node )
{
  if ( node.nextSibling )
    node.parentNode.insertBefore( newNode, node.nextSibling );
  else
    node.parentNode.appendChild( newNode );
}

function clearChildren( node )
{
  while ( node.childNodes.length > 0 )
    node.removeChild( node.childNodes[0] );
}

function removeNode( node )
{
  if ( node.parentNode )
    node.parentNode.removeChild( node );
}

// Prototype JavaScript framework, version 1.4.0
// (c) 2005 Sam Stephenson <sam@conio.net>
function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}