faqts : Computers : Programming : Languages : JavaScript : DHTML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

190 of 261 people (73%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I script BLINK for IE or NN6?

Dec 27th, 2001 23:31
Eric Greenblatt, Martin Honnen,


<HTML>
<HEAD>
<SCRIPT>
function blink (elId) {
  var html = '';
  if (document.all)
    html += 'var el = document.all.' + elId + ';';
  else if (document.getElementById)
    html += 'var el = document.getElementById("' + elId + '");';
  html += 
    'el.style.visibility = ' + 
    'el.style.visibility == "hidden" ? "visible" : "hidden"';
  if (document.all || document.getElementById)
    setInterval(html, 500)
}
function init () {
  blink('aText');
  blink('a2ndText');
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="init();">
<SPAN ID="aText">
<BLINK>
Kibology for all.
</BLINK>
</SPAN>
<BR>
All for Kibology.
<DIV ID="a2ndText">
<BLINK>
Scriptology for all.
</BLINK>
</DIV>
</BODY>
</HTML>
The version above creates a separate interval for each blinking 
element.  I used a different approach in that I keep an associative 
array of the currently blinking elements, and change their visibility 
all at once.  The method provides a parameter to be used to start or 
stop blinking for the element.  When an element stops blinking, it is 
set visible and removed from the array.  When no more elements remain 
blinking, the interval is cleared and the array is initialized.  
Additionally, since there is only one interval in effect, all blinking 
items appear and disappear together.  Notwithstanding the existing 
BLINK tags, for NN4, the element needs to be a positioned (e.g., 
style="position:relative").
/* Starts/stops element blinking */
function blinkText(aObjStr,value)
{
   // Initialize "static" variables
   if (!this.blinkingElements)
   {  this.blinkingElements=new Array();
   }
   if (this.blinkShown==null)
   {  this.blinkShown=true;
   }
   if (!this.blinkInterval)
   {  this.blinkInterval=null;
      if (!aObjStr) // no params specified outside of setInterval, just 
return
      {  return;
      }
   }
   var obj=null, hid="", vis="";
   var numBlinking=0;
   if (aObjStr) // Not called by setInterval
   {  if (!this.blinkingElements[aObjStr])
      {  if (value) // add element to start it blinking
         {  this.blinkingElements[aObjStr]=1;
         }
      } else
      {  if (value) // set existing element to blink
         {  this.blinkingElements[aObjStr]=1;
         } else // existing element to stop blinking and be deleted
         {  this.blinkingElements[aObjStr]=0;
         }
      }
   } else
   {  for (objStr in this.blinkingElements) // toggle all blinking 
elements
      {  if (document.layers) // NN4
         {  obj=eval("document."+objStr);
            vis='obj';
            if (this.blinkingElements[objStr])
            {  hid=this.blinkShown ? "'hide'" : "'show'";
            } else
            {  hid="'show'";
            }
         } else // IE, NN6
         {  obj=eval(document.all ? "document.all."+objStr :
"document.getElementById('"+objStr+"')");
            vis='obj.style';
            if (this.blinkingElements[objStr]) // toggle status if 1
            {  hid=this.blinkShown ? "'hidden'" : "'visible'";
            } else // set to visible if now zero
            {  hid="'visible'";
            }
         }
         var evalStr=vis+".visibility="+hid;
         if (obj && eval(vis)) // ensure object exists
         {
            eval(evalStr); // execute style change
         }
         if (this.blinkingElements[objStr])
         {  numBlinking++; // count number of blinking elements
         } else // remove this element from array
         {  delete this.blinkingElements[objStr];
         }
      }
   }
   if (value) // blinking requested
   {  if (!this.blinkInterval) // not already blinking elements
      {  this.blinkInterval=setInterval("blinkText()",500);
      }
   } else if (!aObjStr && !numBlinking) // no element specified and no 
more elements to blink
   {  clearInterval(this.blinkInterval);
      this.blinkInterval=null;
      this.blinkingElements=new Array();
      this.blinkShown=true;
   } else // simply toggle value for next iteration
   {  this.blinkShown=!this.blinkShown;
   }
}