Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7645853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:03:11+00:00 2026-05-31T10:03:11+00:00

Not Looking for a Use Framework XXX Answer This question is not intended for

  • 0

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]);  
        }
    }
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:03:12+00:00Added an answer on May 31, 2026 at 10:03 am

    A NodeList (such as the one returned by document.getElementsByTagName) is typically a live list — changes you make to the DOM show up in it as well. So each time you add a span before 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 a span, 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) or
      children = 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 - 1 to 0).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently received some advice on a question regarding an easy to use web-framework
I'm looking for a python web framework that is easy to use and allows
Maybe I'm not looking in the right places but I'd like to use SQL
I am looking to use quartz to schedule emails, but I'm not sure which
I'm looking to use NVelocity in my ASP.NET MVC application, not as a view
I'm looking for some advice on whether or not I should use a separate
I'm looking for a profiler to use with native C++. It certainly does not
I was looking at http://tldp.org/LDP/abs/html/why-shell.html and was struck by: When not to use shell
I am looking for the cleanest way. I am tempted to use delegates not
I'm not looking for the usual answer like Web-services. I'm looking for a light

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.