Welcome to bloggerit

Code, examples, rants and stuff

Illegals cost money!  Gwinnett Daily Post ran a story about the cost of Illegal Immigration that got me thinkin...
 
Hiding and Unhiding stuff using javascript(a cross browser solution). This took me time to figure out so I thought I'd share it. It switches out className values instead of using style.display
 
Code Comments. I don't comment my code. Here's why.
 
SQL Server and Today. Here's a handy little view to get today's date. It is as easy as select * from v_today and uses convert and some SQL date functions
 
Posting PHP Forms. The difference between form fields and request variables. Post and Get - you know - that old chestnut.
 
Sites I like or would like you to see. Some I even have a vested interest in......
 
Some of my wife's glass mosaic art. Nice stuff. Tell Michelle you like it.
 
A buddy left me this voicemail. It is an impression of AL GORE and it made me laugh. Be warned: Bad words.

Listen here


Google
 
 

Cross-Browser View/Hide

I just gotta say that this pissed me off to no end. I had code that worked well with IE, Opera and Firefox but Safari just wouldn't work. Most of my work is for Intranets so I could code to IE but once I had to do some public stuff the "squeeze hit the fan" - so to speak.

It turns out that the only way I could get it to work is with a style called hidden. It has one entry: display:none. I had to change the className of the object to hidden and then change it back later on. I also created an entry called shown that also had one entry: display:block that I could use to show an element. Now, in order to change it back I needed to know what it's className was. So here is how I solved for "X"....

I created 2 javascript arrays and a counter to keep track of how many I added:

  var aclasslist = new Array();
  var aidlist = new Array();
  var acount=0;

  
I also created a function called cbtoggle() that accepts on parameter - the objectid. It looks for the element in the arrays and if it does not find it it adds it and it's className to the arrays. If the className is either hidden or blank it adds the value shown.

This also uses a function called findObj a buddy sent me. It seems to return stuff better that getElementById

Now if you are like me you quit reading and just want the damned code. Here it is:


<style>
  .hidden { display:none }
  .shown { display:block }
  
</style>

<a href="javascript:cbtoggle('someelementid')">view/hide element</a>
<div id='someelementid'>some html text and stuff </div>
  
<script language=javascript>

function findObj(theObj, theDoc)                {
        var p, i, foundObj;
        
        if(!theDoc) theDoc = document;

        if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)              {
                theDoc = parent.frames[theObj.substring(p+1)].document;
                theObj = theObj.substring(0,p);
        }

        if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all [theObj];

        for (i=0; !foundObj && i < theDoc.forms.length; i++)
                foundObj = theDoc.forms[i][theObj];
                for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
                        foundObj = findObj(theObj,theDoc.layers[i].document);

        if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);

            if( ! foundObj  ) return false;
            
        return foundObj;
}

var aclasslist = new Array();
var aidlist = new Array();
var acount=0;


function cbtoggle( oid )
{
  if( ! findObj(oid ) ) return;
  
  var o = findObj(oid);
  
  if( acount==0 )
  {
    cbaddid( oid );
  }
  else
  {
     if( cbindex(oid)==-1 )
     {
       cbaddid(oid);
     }
  }
  if( o.className=='hidden' )
  {
     o.className=cbgetclass( oid );     
     
  }  
  else
    o.className='hidden';
    
}

function cbaddid( oid )
{
   aidlist[acount]=oid;
   if( findObj(oid).className != '' && findObj(oid).className !='hidden' )
     aclasslist[acount]=findObj(oid).className;
   else
     aclasslist[acount]='shown';
   acount++;
   
   
}

function cbindex( oid )
{
  for( i=0; i < acount; i++ )
  {
    if( aidlist[i]==oid )
      return i;
  }
  return -1;
}    

function cbgetclass( oid )
{
  var i=cbindex(oid )
  if( i > -1 )
  {
    return aclasslist[i];
  }
  return '';
}


</script>
  
  
Art by Michelle Cormack. I'm fan, you know.
Peace, Love and Harmony
Starbright
Sea Oats

Southern Star Studios: glass art      Toddism.com      lockboxdata.com