/    Sign up×
Community /Pin to ProfileBookmark

[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;}[/CODE]

The first part of the code is a script from Robert Nyman, [url]http://www.robertnyman.com[/url] – 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.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangFeb 02.2010 — 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
Copy linkTweet thisAlerts:
@meekauthorFeb 02.2010 — Hi.

This seems to work fine. Thanks you!
×

Success!

Help @meek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...