<ralphdahlgren.com>


 
Navigation
  • Home
  • Etomite CMS News
  • Code Library
    • Files
      • style.css
      • commonJScripts.js
      • contrastRows.js
  • Tutorials
  • Ralphs Rants
  • Site Map
  • Search Help
  • Login|Logout
 
Credits
 
Valid XHTML 1.1 Valid CSS! Powered By PHP Powered By MySQL
Powered By Etomite
 

commonJScripts.js
File: ./templates/mytpl/commonJScripts.js
// START::Event Handlers
function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be attached");
  }
}

function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}
// END::Event Handlers


// START::XMLHttpRequest function library
// Author: Ralph A. Dahlgren
// Last Modified: 2008-05-23

// global flag
var isIE = false;

// boolean flag which determines if this is an asynchronous request
var asyncDefault = false;

// retrieve XML document (reusable generic function);
// Url parameter is URL string (relative or complete)
// Obj parameter is the id of the element that will receive results
// Method
function runXMLHttpRequest(Method, Url, Async, callbackFunction)
{
  if(Url == '') return false;
  if(Method == '') Method = "GET";
  if(Async == '') Async = asyncDefault;
  if(callbackFunction == '') Async = false;

  if (window.XMLHttpRequest)
  {
    // native XMLHttpRequest object
    req = new XMLHttpRequest();
  }
  else if(window.ActiveXObject)
  {
    // IE/Windows ActiveX object
    req = new ActiveXObject("Microsoft.XMLHTTP");
    isIE = true;
  }
  if(req)
  {
    if(Async) req.onreadystatechange = callbackFunction;
    req.open(Method, Url, Async);
    req.setRequestHeader("If-Modified-Since", "Thu, 1 Jan 1970 00:00:00 GMT");
    req.setRequestHeader("Cache-Control", "no-cache");
    req.send(null);
  }
  if(!Async)
  {
    return req.responseText;
  }
}

function getXMLHttpRequest(Url)
{
  if(Url == '') return false;
  Method = "GET";
  Async = false;

  if (window.XMLHttpRequest)
  {
    // native XMLHttpRequest object
    req = new XMLHttpRequest();
  }
  else if(window.ActiveXObject)
  {
    // IE/Windows ActiveX object
    req = new ActiveXObject("Microsoft.XMLHTTP");
    isIE = true;
  }
  if (req)
  {
    req.open(Method, Url, Async);
    req.setRequestHeader("If-Modified-Since", "Thu, 1 Jan 1970 00:00:00 GMT");
    req.setRequestHeader("Cache-Control", "no-cache");
    req.send(null);
  }
  return req.responseText;
}

function putXMLHttpRequest(Method, Url, Obj, Mode, Async)
{
  if(Url == '' || Obj == '') return false;
  if(Method == '') Method = "GET";
  if(Async == '') Async = asyncDefault;
  if(Mode == '') Mode = 'append';

  if (window.XMLHttpRequest)
  {
    // native XMLHttpRequest object
    req = new XMLHttpRequest();
  }
  else if(window.ActiveXObject)
  {
    // IE/Windows ActiveX object
    req = new ActiveXObject("Microsoft.XMLHTTP");
    isIE = true;
  }
  if (req)
  {
    if(Async) req.onreadystatechange = function()
    {
      // only if req shows "loaded"
      if (req.readyState == 4 && req.status == 200)
      {
        if(Mode == 'append')
        {
          document.getElementById(Obj).appendChild(document.createTextNode(req.responseText));
        }
        else if (Mode == 'write')
        {
          document.getElementById(Obj).innerHTML = req.responseText;
        }
      }
      else if (req.readyState == 4 && req.status != 200)
      {
        alert("Data Retrieval Error:\n" + req.statusText);
      }
    }
    req.open(Method, Url, Async);
    req.setRequestHeader("If-Modified-Since", "Thu, 1 Jan 1970 00:00:00 GMT");
    req.setRequestHeader("Cache-Control", "no-cache");
    req.send(null);
  }
  if(!Async)
  {
    return req.responseText;
  }
}
// END::XMLHttpRequest function library

// Function: extLinks(c)
// Convert all hyperlinks with class="external" to use target="_blank".
// This technique fools HTML markup validators to validate as valid XHTML
// yet allows links to be opened in a new page. By default XHTML does not
// allow the target attribute.
function extLinks(c)
{
  if((c == undefined) || (c == '')) c = 'external';
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++)
  {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") && anchor.className.indexOf(c) >= 0)
    {
      anchor.target = "_blank";
    }
  }
}

window.onload=function(){ extLinks(); }


// Function: contrastRows(tableClass, oddClass, evenClass)
// Author: Ralph A. Dahlgren
// Version: 1.0 - 2008-06-04
// Purpose: displays table rows in alternating contrasting colors based on CSS
// Params:
//   tableClass - the table class name that triggers change (required)
//   oddClass - the CSS class for odd numbered rows (optional)
//   evenClass - the CSS class for even numbered rows (optional)
// Notes:
//   This function should circumvent any of IE's quirks which plague similar
//   scripts. If either oddClass or evenClass are omitted they will use default
//   classes [odd|even].
function contrastRows(tableClass, oddClass, evenClass)
{
  var rowClass = '';
  if(tableClass == undefined || tableClass == '') return;
  if(oddClass == undefined || oddClass == '') oddClass = 'odd';
  if(evenClass == undefined || evenClass == '') evenClass = 'even';
  tables = document.getElementsByTagName('table');
  for(i = 0; i < tables.length; i++)
  {
    if((' '+tables[i].className+' ').indexOf(tableClass) != -1)
    {
      trList = tables[i].getElementsByTagName('tr');
      for(j = 0; j < trList.length; j++)
      {
        tdList = trList[j].getElementsByTagName('td');
        if(tdList.length > 0)
        {
          rowClass = (rowClass == oddClass) ? evenClass : oddClass;
        }
        for(k = 0; k < tdList.length; k++)
        {
          tdList[k].className = tdList[k].className + ' ' + rowClass;
        }
      }
    }
  }
}


Powered by Etomite 1.1 (Prelude).
MySQL: 0.0052 s, 13 request(s), PHP: 0.0251 s, Total: 0.0303 s, document retrieved from database
2009-01-06 10:38 PM