...

View Full Version : Create your own getElementsByClass.



jojsi
04-10-2009, 10:18 AM
I'm in a bit of a problem here! And i wish for someone to help me!
This is what i think i have done.
I made my own getElementsByClass function, put it in a seperate js file.
Created one more whit a function for my specified class "external".
By nothing happens.

This is my function created for finding different classes (core.js), witch i revered to in my HTML file. (<script type="text/javascript" src="core.js">)



Core.getElementsByClass = function(theClass)
{
var elementArray = [];

if (typeof document.all!= "undefined")
{
elementArray = document.all;
}
else
{
elementArray = document.getElementsByTagName("*");
}

var matchedArray =[];
var pattern = new RegExp("(^| )" + theClass + "( |$)");

for (var i =0; i < elementArray.length; i++)
{
if (pattern.test(elementArray[i].className))
{
matchedArray[matchedArray.length] = elementArray[i];
}
}

return matchedArray;
};


And here's my other function in the file javascript.js, <script type="text/javascript" src="javascript.js"> (placed after the core.js) witch i want to execute while clicking on the links of the class "external".



var TheLink =
{
init: function()
{
var link = Core.getElementsByClass("external");
for (var i = 0; i <link.length; i++)
{
link[i].onclick = TheLink.externalLink;
}
},

externalLink: function()
{
return confirm("Vill du verkligen?");
}
};

Core.start(TheLink);

But nothings happens. I'm a to much of an beginner whit javascript. And my error massage the core is not defined don't make any sense to me.

I would be pleased if someone could help me solve my problem and make the function work! (witch i'd almost created from reading a book, that makes it even more strange.) /// Jen.

abduraooft
04-10-2009, 11:21 AM
It's already created and fine tuned at http://www.codingforums.com/showthread.php?t=154727 :)

Kor
04-10-2009, 11:38 AM
And one of the final development versions is:


<script type="text/javascript">
onload=function(){
if (!document.getElementsByClassName) {
document.getElementsByClassName = function (cn) {
var rx = new RegExp("(?:^|\\s)" + cn+ "(?:$|\\s)");
var allT = document.getElementsByTagName("*"), allCN = [], ac="", i = 0, a;
while (a = allT[i=i+1]) {
ac=a.className;
if ( ac && ac.indexOf(cn) !==-1) {
if(ac===cn){ allCN[allCN.length] = a; continue; }
rx.test(ac) ? (allCN[allCN.length] = a) : 0;
}
}
return allCN;
}
}
}
</script>

jojsi
04-10-2009, 01:28 PM
Thanks, but even if i replace my own code to this one, it doesn't work. :S
When I'm using the function. Do i call it the right way in the function i want to execute while clicking the links? I also have to have the javascript in a external file, because of that i made one file for the getelementbyclassname and one for my other function. I'm just really confused about how it works!?

Kor
04-10-2009, 03:43 PM
I also have to have the javascript in a external file, because of that i made one file for the getelementbyclassname and one for my other function. I'm just really confused about how it works!?
Then put it in an external, what prevents you from doing so?


i want to execute while clicking the links

Then execute it while clicking the links, what prevents you from doing so?



function elByClass(){
if (!document.getElementsByClassName) {
document.getElementsByClassName = function (cn) {
var rx = new RegExp("(?:^|\\s)" + cn+ "(?:$|\\s)");
var allT = document.getElementsByTagName("*"), allCN = [], ac="", i = 0, a;
while (a = allT[i=i+1]) {
ac=a.className;
if ( ac && ac.indexOf(cn) !==-1) {
if(ac===cn){ allCN[allCN.length] = a; continue; }
rx.test(ac) ? (allCN[allCN.length] = a) : 0;
}
}
return allCN;
}
}
}

function attachClick(){
var links=document.getElementsByClassName('external'), i=0, l;
while(l=links[i++]){
l.onclick=function(){return confirm("Vill du verkligen?")}
}
}

onload=function(){
elByClass();
attachClick();
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum