Here is an implementation based on jQuery 1.12.4 implementation. It uses getElementsByTagName if available. If not, it uses querySelectorAll, if available. If not, it returns to a recursive traversal. jQuery 1.12.4 supports older browsers such as IE6, according to.
function getElementsByTagName( node, tagName ) { if (tagName == '*') { tagName = undefined; } var merge = function( first, second ) { var len = +second.length, j = 0, i = first.length; while ( j < len ) { first[ i++ ] = second[ j++ ]; } // Support: IE<9 // Workaround casting of .length to NaN on otherwise arraylike objects (eg, NodeLists) if ( len !== len ) { while ( second[ j ] !== undefined ) { first[ i++ ] = second[ j++ ]; } } first.length = i; return first; }, nodeName = function( elem, name ) { return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase(); }, elems, elem, i = 0, context = node, tag = tagName, found = typeof context.getElementsByTagName !== "undefined" ? context.getElementsByTagName( tag || "*" ) : typeof context.querySelectorAll !== "undefined" ? context.querySelectorAll( tag || "*" ) : undefined; if ( !found ) { for ( found = [], elems = context.childNodes || context; ( elem = elems[ i ] ) != null; i++ ) { if ( !tag || nodeName( elem, tag ) ) { found.push( elem ); } else { merge( found, getElementsByTagName( elem, tag ) ); } } } return found; /* return tag === undefined || tag && nodeName( context, tag ) ? merge( [ context ], found ) : found;*/ }
I used the internal getAll () function for jQuery 1.12.4 and copied two helper functions into it (jQuery.nodeName and jQuery.merge). I also made sure that you can name it "*" as tagName by adding a few lines at the top of the function. Finally, at the end of the function, I commented on some functions that add the current node to the result (if it matches) and simply returns the found nodes.
Remember that a function returns an HTMLCollection in some cases, and Array in other cases. Also be careful when "*" is passed as a tag, the output differs depending on the browser: Element.prototype.getElementsByTagName does not return TextNodes, but does a recursive traversal.
Alternatively you can use picoQuery . picoQuery is a jQuery implementation where you can choose which methods you need in the online linker. in this case, you do not need methods, since the selection is part of the kernel, and the assembly is only 1kb gzipped. picoQuery is written for modern browsers, but returns to jQuery 1.12.4 for older browsers.