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

Natalie Downe http://natbat.co.uk/

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

JS: fast through a list

var i = list.length;
for(i; i> 0; i--) {
	alert(i)
}

Object paternity

// object paternity in Javascript

// relationship of object 1 to object 2
function objectPaternity(obj1, obj2) {
 // given an object this function returns 'child' if obj1 is a child of obj2
 // and 'parent' if obj1 is a parent of obj2 - then 'no' if neither
 var relationship = 'no';
 var currentElement = obj1;
 
 while((typeof currentElement.parentNode != 'undefined') && (currentElement != document.body)) {
  // first cycle through all the parent elements of object 1 to see if object 2 is one of them
  if(currentElement.parentNode == obj2) {
   relationship = 'child';
  }
  // walk up the tree and try again
  currentElement = currentElement.parentNode;
 }
 // if object 1 is not a child of object 2 then we test the other way arround
 if(relationship == 'no') {
  currentElement = obj2;
  while((typeof currentElement.parentNode != 'undefined') && (currentElement != document.body)) {
   // now cycle through all the parent elements of object 2 to see if object 1 is one of them
   if(currentElement.parentNode == obj1) {
    relationship = 'parent';
   }
   // walk up the tree and try again
   currentElement = currentElement.parentNode;
  }
 }
 return relationship;
}

Browser detect for IE versions

// is called when you want to test which version of IE a user has, returns true or false

function isIE(versionNumber) {
var detect = navigator.userAgent.toLowerCase();
if(!(navigator && navigator.userAgent && navigator.userAgent.toLowerCase)) {
  	        return false;
  	    } else {
  	        if(detect.indexOf('msie') + 1) {
  	            // browser is internet explorer
  	            var ver = function() {
  	                // http://msdn.microsoft.com/workshop/author/dhtml/overview/browserdetection.asp
  	                // Returns the version of Internet Explorer or a -1
  	                // (indicating the use of another browser).
  	                var rv = -1; // Return value assumes failure
  	                if (navigator.appName == 'Microsoft Internet Explorer') {
  	                    var ua = navigator.userAgent;
  	                    var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  	                    if (re.exec(ua) != null) {
  	                        rv = parseFloat( RegExp.$1 );
  	                    }
  	                }
  	                return rv;
  	            };
  	            var valid = true;
  	            // if the version can be found and the version is less than our version number it is invalid
  	            if ((ver > -1) && (ver < versionNumber)) {
  	                valid = false;
  	            }
  	            return valid;
  	        } else {
  	            return false
  	        }
  	    }
} 

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