Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Roy Marchand http://www.expertsrt.com

« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS 

Bullets Without a UL Tag

There are times when you want to put a bullet on an HTML page but you do not want to use a UL tag... this is how you do it:

<html>
<head>
</head>
<body>
<span style="font-size: 20px;">&bull;a</span><br />
<span style="font-size: 25px;">&bull;b</span><br />
<span style="font-size: 30px;">&bull;c</span><br />
<span style="font-size: 35px;">&bull;d</span><br />
</body>
</html>


For more tips on HTML, CSS, and most Web technologies visit me at ERT

Making Columns Render with Equal Height

A problem sometimes faced by web developers is trying to get two (or more) columns in a multi-column layout to be the same height when the content is variable. Rather than using an arbitrary hardcoded value, the heights can be equalized (to the tallest one) with this script.

Assuming two columns in div tags with ids of "leftside" and "rightside" this script will set the height of the shorter to the height of the taller one. The page must be in standards compliant mode with a valid doctype or IE will mess it up in quirks mode. For longer articles and discussion visit my home site... ERT
<script type="text/javascript">
function setH()
{
   var maxH = Math.max(document.getElementById('leftside').offsetHeight,document.getElementById('rightside').offsetHeight);
   document.getElementById('leftside').style.height=maxH+'px';
   document.getElementById('rightside').style.height=maxH+'px';
}
onload=setH;
</script>

Detecting Mouse Position

This is an easy way to determine the mouse position (click anywhere and the co-ordinates get displayed:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<head>
   <title>Mouse co-ordinates</title>
   <style type="text/CSS">
      .holder {background-color:lightyellow;color:blue;width:40}
   </style>
<script type="text/javascript">
function showit()
{
   document.forms['theform'].xcoord.value=event.x;
   document.getElementById('spanx').innerHTML='x='+event.x;
   document.forms.theform.ycoord.value=event.y;
   document.getElementById('spany').innerHTML='y='+event.y;
}
function showitMOZ(e)
{
   document.forms['theform'].xcoord.value=e.pageX;
   document.getElementById('spanx').innerHTML='x='+e.pageX;
   document.getElementById('spany').innerHTML='y='+e.pageY;
   document.forms.theform.ycoord.value=e.pageY;
}
if (!document.all){
window.captureEvents(Event.CLICK);
window.onclick=showitMOZ;
}
else
{
document.onclick=showit;
}
</script>
</head>
<body>
<br><br><br>
<h1>You can store them in form fields</h1>
<form name="theform">
x = <input name="xcoord" style="width:40"> 
y = <input name="ycoord" style="width:40">
</form>
<br /><br />
<h1> or as plain text</h1>
<span id="spanx">&nbsp;</span>
<span id="spany">&nbsp;</span>
</body>
</html>



Of course knowing the co-ordinates also makes it possible to position elements based on the position of the mouse pointer. It will work for any mouse event Mouseover, mouseout, etc; just change click to the desired event in the final if statement of the script. More good stuff and articles at the home of real IT Mentors

Dynamic Modification of CSS Class Properties

Modifying the rendering of an element with an ID is relatively easy using document.getElementById. Modifying the attributes fo an entire class dynamically is a little more code, but the DOM does provide the means to do it through the stylesheet object; with some cross-browser variations.

For additional CSS and HTML see some of the full length articles
Written by IT professionals from Experts Round Table

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<head>
<title> Fragment</title>
<style type="text/css">
A.topLinks {font-weight:bold}
.Borders {font-weight:bold}
</style>
<script type="text/javascript">
function modRule()
{
	if (!document.styleSheets) return;
	var thecss = new Array();
	if (document.styleSheets[0].cssRules)  // Standards Compliant
        {
	   thecss = document.styleSheets[0].cssRules;
        }
	else
        {         
           thecss = document.styleSheets[0].rules;  // IE 
        }
        for (i=0;i<thecss.length;i++)
        {
           if ((thecss[i].selectorText.toLowerCase()=='a.toplinks') || (thecss[i].selectorText.toLowerCase()=='.borders'))
           {
	     thecss[i].style.cssText="font-weight:normal; color:red";
           }
        }
}
</script>
</head>
<body>
<a href="#" class="topLinks" onclick="modRule();return false;">click here</a>
<div class="Borders"> hello world. <span style="color:blue">All of your bases are belong to us.</span> </div>
</body>
</html>


Notice that the color of the text in the span is not changed, because the inline style prevails over the class in the stylesheet. This technique can be used to make on the fly changes to the look and feel of a page in response to events or user input.

Setting Element Height Dynamically

There are times when you need to control the height of an element based on the screen size. However youcan control the user setup, and of course there are browser differences so this snippet handles browser and object detection and sets the height during page load and for any re-sizing event.

An example of the snippet in action

  var minorOffset = (document.all)? 25 : 38;
   function setHgt()
   {
      var sHGT;
      srcobj=document.getElementById('main');
      if (self.innerHeight)
      {
	   sHGT = self.innerHeight;
	}
      else
      { 
         if (document.documentElement && document.documentElement.clientHeight)
         {
	      sHGT = document.documentElement.clientHeight;
         }
         else
         {
            if (document.body)
            {
              sHGT = document.body.clientHeight;
            }
         }
      }
      sHGT=sHGT-(document.getElementById('main').offsetTop+minorOffset);
  document.getElementById('main').style.height=sHGT+"px";
   }
window.onload=setHgt;
window.onresize=setHgt;

Typed Tooltip Effect

This simple bit of scripting will create a dynamic typing effect for your tool tips.
The script can be
seen implemented here



var str='';
var obj='false';
var pntr='';
var cnt=0;
var textArr = new Array(5);
textArr[0] = "All your bases are belong to us!";
textArr[1] = "The answer is 42; of course.";
textArr[2] = "The end of the universe will be the result of a mis-calculation in the core.";
textArr[3] = "I don't believe its alive; but it thinks it is.";
textArr[4] = "About 10 seconds after you open it; you'll wish you hadn't";

function looptext()
{
   obj.innerHTML=str.substr(0,cnt);
   cnt++;
   if (cnt<=str.length) setTimeout('looptext()',100);
}

function typeText(lnk,lyr)
{
   str=textArr[lyr];
   pntr= 'lyr'+lyr;
   obj=document.getElementById(pntr);
   obj.style.left=(lnk.offsetLeft+30)+'px';
   obj.style.top=(lnk.offsetTop+25)+'px';
   obj.style.display='block';
   cnt=1;
   looptext();
}

function clearText(lyr)
{
   cnt=2500;
   pntr='lyr'+lyr;
   obj=document.getElementById(pntr);
   obj.style.display='none';
   obj.style.left='-50px';
   obj.style.top='-50px';
   obj.innerHTML='&nbsp;';
}


The links using the effect are done this way:

   <a href="javascript:alert('no page linked')"
      onmouseover="typeText(this,2)"
      onmouseout="clearText(2)"
      onclick="clearText(2)"> 
   This is link Three
   </a>

Expand and collapse effect for data grids

This is a little use of dynamic CSS to create expandable, and collapsible sections in data grid presentations.
Cd&
Experts Round Table


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html>
<head>
<style type="text/CSS">
   table {
          width:100%; 
          border-collapse:collapse;
          border:2px solid silver;
         }
   tbody {
          display:block
         }
   th {
       font-weight:normal;
       text-align:left;
      }
   td {
       text-align:center;
       padding:8px;
       border:1px solid silver; }
   .linkspan {
              color:gold;
              background-color:blue;
              font-weight:bold;
              text-decoration:none;
              padding:0 2px; 
              font-size:1.2em;
              margin:right:3px;
             }
   .vis {
         display:block;
        }
</style> 

<script type="text/javascript"> 
   var ELpntr=false;
   function hideall()
   {
      locl = document.getElementsByTagName('tbody');
      for (i=0;i<locl.length;i++)
      {
         locl[i].style.display='none';
      }
   }

   function showHide(EL,PM)
   {
      ELpntr=document.getElementById(EL);
      if (ELpntr.style.display=='none')
      {
         document.getElementById(PM).innerHTML=' - ';
         ELpntr.style.display='block';
      }
      else
      {
         document.getElementById(PM).innerHTML=' + ';
         ELpntr.style.display='none';
      }
   }
   onload=hideall;
</script>
</head> 
<body> 
<table>
   <thead>
   <tr class="vis"> <th colspan="3"><a href="#" 
      onclick="showHide('fruit','fruitspan')">
        <span id="fruitspan" class="linkspan"> + </span> Fruit:</a></th></tr>
   </thead> 
   <tbody id="fruit">
      <tr> <td> Apple</td><td> Red</td><td> Shiny</td></tr>
      <tr> <td> Pear</td><td> Green</td><td> Firm</td></tr>
      <tr> <td> Banana</td><td> Red</td><td> Shiny</td></tr>
   </tbody>
</table>
<table>
   <thead>
   <tr class="vis"> <th colspan="3"><a href="#" onclick="showHide('vegtable','vegtablespan')">
        <span id="vegtablespan" class="linkspan"> + </span> Vegtable:</a></th></tr> 
   </thead>
   <tbody id="vegtable">
      <tr> <td> Carrot</td><td> Orange</td><td> Crisp</td></tr>
      <tr> <td> Cucumber</td><td> Green</td><td> Delicious</td></tr>
      <tr> <td> Cauliflower</td><td> White</td><td> Firm</td></tr>
   </tbody>
</table>
<table>
   <thead>
   <tr class="vis"> <th colspan="3"><a href="#" onclick="showHide('animal','animalspan')">
        <span id="animalspan" class="linkspan"> + </span> Animal:</a></th></tr>
   </thead> 
   <tbody id="animal">
      <tr> <td> Cat</td><td> Calico</td><td> Lazy</td></tr>
      <tr> <td> Dog</td><td> Retriever</td><td> Golden</td></tr>
      <tr> <td> Badger</td><td> Muddy</td><td> Mean</td></tr>
   </tbody>
</table> 
</body> 
</html> 

Passing parameters between pages without servers side scripting

// description of your code here
Sometimes you need to pass parameters directly between pages for dynamic effect. This is the way to process those on the receiving page. The arguments can be added directly onto the url or a form with a get method caa be used. As long as the server just passs the request through the receiving page can use the parameters to present alternate presentations, pre-populate form eleemnts, or just carry through the values for a multi-page form.

Cd&
Experts Round Table

<html> 
<head> 
<script type="text/javascript"> 
   parmarr = new Array;
   valuearr = new Array;
   function readparms()
   {
      if(location.search!='')
      { 
         Args = location.search.substring(1); 
         parmarr = Args.split('&');
         for(i=0;i<parmarr.length;i++)
         {
            valuearr[i] = parmarr[i].split('='); 
         }
      } 
   }
</script> 
</head> 
<body onLoad="readparms();showparms();"> 
<script type="text/javascript">
   // all this script does is display the parms but the
   // stored values can be used by any function
   function showparms()
   { 
      if(location.search!='')
      { 
         for(i=0;i<valuearr.length;i++)
         {
            document.write(valuearr[i][0] +'=' +valuearr[i][1] +'<br>'); 
         }
      } 
   } 
</script>  
</body> 
</html>

Add Rows to an HTML table Dynamically

// description of your code here
This script modifies an existing table in a page using
the DOM methods to add rows

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html>
<head>
<script type="text/javascript">
function addRow(content,morecontent)
{
         if (!document.getElementsByTagName) return;
         tabBody=document.getElementsByTagName("TBODY").item(0);
         row=document.createElement("TR");
         cell1 = document.createElement("TD");
         cell2 = document.createElement("TD");
         textnode1=document.createTextNode(content);
         textnode2=document.createTextNode(morecontent);
         cell1.appendChild(textnode1);
         cell2.appendChild(textnode2);
         row.appendChild(cell1);
         row.appendChild(cell2);
         tabBody.appendChild(row);
       
   
}
</script>
</head>
<body>
<table border='1' id='mytable'>
<tbody>
<tr><td>22</td><td>333</td></tr>
<tr><td>22</td><td>333</td></tr>
</tbody>
</table>
<button onClick='addRow("123","456");return false;'>
Add Row</button>
</body>
</html>

Javascript to put a hotkey on a Web Page

// description of your code here
This page will fire an event when the key specified in the
variable key1 is pressed.

Cd&
http://www.expertsrt.com

var key1="32";
var x='';
function handler(e) 
{
  if (document.all) {
  var evnt = window.event; 
  x=evnt.keyCode;
}
else
x=e.charCode;
if (x==key1) location.href='http://www.expertsrt.com';
}
if (!document.all){
window.captureEvents(Event.KEYPRESS);
window.onkeypress=handler;
}
else
{
document.onkeypress = handler;
} 
« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS