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

Alan Coleman www.alancoleman.co.uk

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

Finding the URL presented to the browser

Finding the URL presented to the browser, instead of the actual location of the page.

   1  
   2  <SCRIPT LANGUAGE="JavaScript">
   3    
   4        <!--
   5        {
   6         document.write(location.href);
   7        }
   8        // -->
   9  </SCRIPT>

Giving form labels a width

For some reason this has always bugged me in the past. When using a label they don't comply with width unless accompanied by a float. I've used em to define the width so that is conforms with browser text settings. Dead simple

   1  
   2  label {
   3  	margin-right:10px;
   4  	width:10em;
   5  	float:left;
   6  }
   7  

CSS include

Import the contents of an existing CSS file into your current CSS

   1  
   2  @import "general.css";

Javascript document text replace

This will find any instance of a chosen word and swap it out for another (non case sensitive). It comes courtesy of a moderator on webdeveloper.com

   1  
   2  <script type="text/javascript">
   3  var words={
   4  'Bill':'William','Miss':'Mrs'
   5  }
   6  var regs=[];
   7  for(arg in words){regs[regs.length]=new RegExp(arg,'g')}
   8  
   9  window.onload=function replaceText(){
  10  var tags=document.getElementsByTagName('body')[0].getElementsByTagName('*');
  11  var i=0,t;
  12  	while(t=tags[i++]){
  13  		if(t.childNodes[0]){
  14  			var j=0, c;
  15  			while(c=t.childNodes[j++]){
  16  				if(c.nodeType==3){
  17  					var k=0;
  18  					for(arg in words){
  19  						c.nodeValue=c.nodeValue.replace(regs[k],words[arg]);
  20  						k++;
  21  					}
  22  				}
  23  			}
  24  		}
  25  	}
  26  }

target= not valid in XHTML

Used for pages with strict doctypes (i.e. no target="_blank"). Automagically adds them back in to links with rel="external"

   1  
   2  function externalLinks() {
   3   if (!document.getElementsByTagName) return;
   4   var anchors = document.getElementsByTagName("a");
   5   for (var i=0; i<anchors.length; i++) {
   6     var anchor = anchors[i];
   7     if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
   8       anchor.target = "_blank";
   9      }
  10  
  11  }
  12  }
  13  window.onload = externalLinks;
  14  
  15  //////////////
  16  
  17  <a href="whatever" rel="external" title="whatever">Whatever</a>
  18  

Form name not valid in strict XHTML

<form name="search" id="search"...

'name' is not valid in strict XHTML. So instead use the id

   1  
   2  <form id="search"...
   3  
   4  <a href="#" onclick="document.forms['search'].submit();return false" title="Search">Search</a>

DOM Image rollover

This is by Chris Poole (http://chrispoole.com) and it works really well.

   1  
   2  function domRollover() {
   3  if (navigator.userAgent.match(/Opera (\S+)/)) {
   4  var operaVersion = parseInt(navigator.userAgent.match(/Opera (\S+)/)[1]);
   5  }
   6  if (!document.getElementById||operaVersion <7) return;
   7  var imgarr=document.getElementsByTagName('img');
   8  var imgPreload=new Array();
   9  var imgSrc=new Array();
  10  var imgClass=new Array();
  11  for (i=0;i<imgarr.length;i++){
  12  if (imgarr[i].className.indexOf('domroll')!=-1){
  13  imgSrc[i]=imgarr[i].getAttribute('src');
  14  imgClass[i]=imgarr[i].className;
  15  imgPreload[i]=new Image();
  16  if (imgClass[i].match(/domroll (\S+)/)) {
  17  imgPreload[i].src = imgClass[i].match(/domroll (\S+)/)[1]
  18  }
  19  imgarr[i].setAttribute('xsrc', imgSrc[i]);
  20  imgarr[i].onmouseover=function(){
  21  this.setAttribute('src',this.className.match(/domroll (\S+)/)[1])
  22  }
  23  imgarr[i].onmouseout=function(){
  24  this.setAttribute('src',this.getAttribute('xsrc'))
  25  }
  26  }
  27  }
  28  }
  29  domRollover();
  30  
  31  //
  32  
  33  Where your image would look like this:
  34  
  35  <img src="images/b_news.gif" alt="News" class="domroll images/b_news_roll.gif" />
  36  
  37  
  38  

Remove file extension from getAttribute('src')

This should set x to anything before an instance of the string ".gif"

   1  
   2    x=getAttribute('src')
   3    x=x.substring(0, x.indexOf('.gif'));

indexOf - if part of a string contains "Text" for example.

So here, if the string pageName contains "Text" the statement will output positive


   1  
   2  if (pageName.indexOf("Text")!= -1)

Formatting large numbers with a javascript function

This function will format a large number like 23000456 to 23,000,456

   1  
   2  function addCommas(nStr)
   3  {
   4  	nStr += '';
   5  	x = nStr.split('.');
   6  	x1 = x[0];
   7  	x2 = x.length > 1 ? '.' + x[1] : '';
   8  	var rgx = /(\d+)(\d{3})/;
   9  	while (rgx.test(x1)) {
  10  		x1 = x1.replace(rgx, '$1' + ',' + '$2');
  11  	}
  12  	return x1 + x2;
  13  }
  14  
  15          example = addCommas(example);
  16  
« Newer Snippets
Older Snippets »
Showing 1-10 of 20 total  RSS