Thành viên:AmieKim/util.js

Tủ sách mở Wikibooks

Chú ý: Sau khi lưu trang, có thể bạn sẽ phải xóa bộ nhớ đệm của trình duyệt để xem các thay đổi.

  • Firefox / Safari: Nhấn giữ phím Shift trong khi nhấn Tải lại (Reload), hoặc nhấn tổ hợp Ctrl-F5 hay Ctrl-R (⌘R trên Mac)
  • Google Chrome: Nhấn tổ hợp Ctrl-Shift-R (⇧⌘R trên Mac)
  • Internet Explorer / Edge: Nhấn giữ phím Ctrl trong khi nhấn Làm tươi (Refresh), hoặc nhấn tổ hợp Ctrl-F5
  • Opera: Nhấn tổ hợp Ctrl-F5.
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;
}