Solution to XML namespace problem in getElementsByTagName

2Milinda31st Jul 2008JavaScript

Here is a simple code I used as a solution to XML namespace handling in different browsers(IE, Firefox 2, Firefox3, ..). But it requires more arguments like namespace and namespace frefix. But I think that this function can be improve based on problem that you have to solve.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function getElementsByTagName(tagName, ns, prefix, scope){
		var elementListForReturn = scope.getElementsByTagName(prefix+":"+tagName);
		if(elementListForReturn.length == 0){
			elementListForReturn = scope.getElementsByTagName(tagName);
			if(elementListForReturn.length == 0){
				elementListForReturn = scope.getElementsByTagName("ns:"+tagName);
				if(elementListForReturn.length == 0 && document.getElementsByTagNameNS){
					elementListForReturn = scope.getElementsByTagNameNS(ns, tagName);
				}
			}
		}     
 
		return elementListForReturn;
    }
Related Posts: Sphere: Related Content

Related posts brought to you by Yet Another Related Posts Plugin.

2 Comments Comments Feed

  1. Aamir Afridi (September 15, 2008, 5:05 pm).

    It seems nice but can you please give any example to use this function.

  2. Marjorie (July 2, 2009, 7:47 pm).

    Thanks so much for the code. I had been struggling to get this slideshow from an RSS feed to work in IE, but the namespaces had me stumped. http://www.plaidhandprint.com/myshow.php?Deviant=flowersdaughter

Add a Comment