Not Looking for a Use Framework XXX Answer
This question is not intended for finding a practical solution via a framework. Answering with use framework XXX, or this is so easy in framework XXX, or why not use this framework XXX??? doesn’t answer the question.
I have a function meant to run after a page has been loaded: performShim. This function iterates over all elements in the DOM that are span tags, checks if they have a className of shim and if so, calls shim passing to it a reference of the matched element.
My goal was to prepend another span that contains an iframe to the element that is passed to shim.
With the code I wrote so far, I am able to append to the element’s parent just fine. However, if I comment out the append line and instead try the prepend line the browser hangs in presumably an infinite-loop.
It’s not readily obvious to me why this is the case.
function shim( element ) {
var iframe = document.createElement('iframe');
iframe.setAttribute( 'frameborder', '0' );
iframe.setAttribute( 'scrolling', 'no' );
iframe.setAttribute( 'align', 'bottom' );
iframe.setAttribute( 'marginheight', '0' );
iframe.setAttribute( 'marginwidth', '0' );
iframe.setAttribute( 'src', "javascript:'';" );
var span = document.createElement('span');
span.appendChild(iframe);
//element.parentNode.insertBefore(span,element); //causes infinite loop?
element.parentNode.appendChild(span); //this line works OK
var els = element.style;
var originalVisibility = els.visibility;
var originalPosition = els.position;
var originalDisplay = els.display;
els.visibility = 'hidden';
els.position = 'absolute';
els.display = 'inline';
var width = element.offsetWidth;
var height = element.offsetHeight;
els.display = originalDisplay;
els.position = originalPosition;
els.visibility = originalVisibility;
iframe.style.width = (width-6) + 'px';
iframe.style.height = (height-6) + 'px';
}
function performShim() {
var children = document.getElementsByTagName("span");
for( var i = 0; i < children.length; i++ ) {
if( children[i].className == "shim" ) {
shim(children[i]);
}
}
}
A
NodeList(such as the one returned bydocument.getElementsByTagName) is typically a live list — changes you make to the DOM show up in it as well. So each time you add aspanbefore the current one, you’re extending the list by one element and moving the current element over by one, and the next iteration puts you right back at the node you just finished.You have a couple of easy workarounds for that…
Bump the counter when you add a node.(Ugly, and if you ever end up adding something instead of aspan, you’ll end up skipping nodes and it won’t be obvious why.)Copy the list to an array and iterate over the array. You could do this with something like
children = [].slice.call(children, 0);(more common) orchildren = Array.apply(window, children);.Use
document.querySelectorAll, which returns you a NodeList that’s not live. (And even if it were live, in this case you could select'span.shim'and the inserted spans wouldn’t show up in it anyway.)Iterate backwards (from
children.length - 1to 0).