Question

My following code will only alert once from Firebug, using jQuery's append. I was pretty certain anytime a script tag hits the DOM, it executes immediately - iframe or not.

var iframe        = document.getElementById('myiframe'),
    iframe_window = iframe.contentWindow,
    iframe_head   = iframe_window.document.getElementsByTagName( 'head' )[0],
    script        = iframe_window.document.createElement('script');

script.type      = 'text/javascript';
script.innerHTML = "alert(1);";

iframe_head.appendChild( script.cloneNode( true ) );
$(iframe_head).append( script.cloneNode( true ));

For added confusion I used jsfiddle ( http://jsfiddle.net/vs8KV/ ) and it DID alert twice. Do you think this is a Firebug issue or something else?

Was it helpful?

Solution

It seems I left out an important detail... the iframe is an RTE. This means the iframe has designMode turned on, which in turn disallows JS from running inside. I had no idea about that before today.

It would seem that the JS was running through jquery by running in the parent instead. If I console logged the window from the jquery.append it would show the parent window and not the iframe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top