www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] If element exists, run script... How?

  1. #1
    Join Date
    Dec 2004
    Posts
    88

    resolved [RESOLVED] If element exists, run script... How?

    Hi.

    I'm using this script to make some changes to a table which is only accessible via a class serverside:

    Code:
    /*
    	Developed by Robert Nyman, http://www.robertnyman.com
    	Code/licensing: http://code.google.com/p/getelementsbyclassname/
    */	
    var getElementsByClassName = function (className, tag, elm){
    	if (document.getElementsByClassName) {
    		getElementsByClassName = function (className, tag, elm) {
    			elm = elm || document;
    			var elements = elm.getElementsByClassName(className),
    				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
    				returnElements = [],
    				current;
    			for(var i=0, il=elements.length; i<il; i+=1){
    				current = elements[i];
    				if(!nodeName || nodeName.test(current.nodeName)) {
    					returnElements.push(current);
    				}
    			}
    			return returnElements;
    		};
    	}
    	else if (document.evaluate) {
    		getElementsByClassName = function (className, tag, elm) {
    			tag = tag || "*";
    			elm = elm || document;
    			var classes = className.split(" "),
    				classesToCheck = "",
    				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
    				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
    				returnElements = [],
    				elements,
    				node;
    			for(var j=0, jl=classes.length; j<jl; j+=1){
    				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
    			}
    			try	{
    				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
    			}
    			catch (e) {
    				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
    			}
    			while ((node = elements.iterateNext())) {
    				returnElements.push(node);
    			}
    			return returnElements;
    		};
    	}
    	else {
    		getElementsByClassName = function (className, tag, elm) {
    			tag = tag || "*";
    			elm = elm || document;
    			var classes = className.split(" "),
    				classesToCheck = [],
    				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
    				current,
    				returnElements = [],
    				match;
    			for(var k=0, kl=classes.length; k<kl; k+=1){
    				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
    			}
    			for(var l=0, ll=elements.length; l<ll; l+=1){
    				current = elements[l];
    				match = false;
    				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
    					match = classesToCheck[m].test(current.className);
    					if (!match) {
    						break;
    					}
    				}
    				if (match) {
    					returnElements.push(current);
    				}
    			}
    			return returnElements;
    		};
    	}
    	return getElementsByClassName(className, tag, elm);
    };
    
    function replace_stuff () {
     getElementsByClassName("login-headline","table")[0].border = 0;}
    The first part of the code is a script from Robert Nyman, http://www.robertnyman.com - my function is added at the end of the script. It works fine, but my page is dynamically generated and sometimes this element with the class name doesn't exist. This gives an error when the page is loaded: "getElementsByClassName("login-headline", "table")[0] is undefined". How can my function be rewritten so that it checks to see if the class is avaliable before running the script?

    Hope this makes sense.

    Thanks.

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    function replace_stuff () {
    var aList = getElementsByClassName("login-headline","table");
    if(aList != '') {
        aList[0].border = 0;
        }
    }
    You may also need to check on the length of the list before using an index

  3. #3
    Join Date
    Dec 2004
    Posts
    88
    Hi.

    This seems to work fine. Thanks you!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles