Web Analytics Made Easy -
StatCounter Create your own getElementsByClass. - CodingForum

Announcement

Collapse
No announcement yet.

Create your own getElementsByClass.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Create your own getElementsByClass.

    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">)

    Code:
    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".

    Code:
    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.

  • #2
    It's already created and fine tuned at http://www.codingforum.net/showthread.php?t=154727
    Digitalocean Cloud Hosting (Referral link - get $10 free credit) Fameco

    Comment


    • #3
      And one of the final development versions is:
      Code:
      <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>
      KOR
      Offshore programming
      -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

      Comment


      • #4
        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!?

        Comment


        • #5
          Originally posted by jojsi View Post
          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?
          Originally posted by jojsi View Post
          i want to execute while clicking the links
          Then execute it while clicking the links, what prevents you from doing so?
          Code:
          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();
          }
          Last edited by Kor; Apr 10, 2009, 09:45 AM.
          KOR
          Offshore programming
          -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

          Comment

          Working...
          X