Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 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
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,854
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    It's already created and fine tuned at document.getElementsByClassName()
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!?

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote 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?
    Quote 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; 04-10-2009 at 02:45 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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