jabal
jabal

Reputation: 12367

IE dynamically created Iframe's onload function never called

I am struggling with the issue that I am unable to attach any onload function to my dynamically created iframe. This happens only in IE, works in FF. Here is my code:

var ifrm = document.createElement("IFRAME");
ifrm.setAttribute("src", "legacy-hostpage.html?rnd=" + new Date().getTime());
ifrm.style.width = "100%";
ifrm.style.height = "400px";
ifrm.name = "legacy-frame";
ifrm.id = "IFRM-" + new Date().getTime();

ifrm.onload = function() {
  alert('onload onload');
};

wrapper.appendChild(ifrm); 

I wish I could use jQuery in this project but I can't.. Can you tell me what's wrong?

Upvotes: 3

Views: 4013

Answers (2)

emre nevayeshirazi
emre nevayeshirazi

Reputation: 19261

I think you should use attachEvent function for IE.

  function myLoad() {
      alert( "onload onload" );
  }

  if ( window.addEventListener ) { 
      ifrm.addEventListener( "load", myLoad, false );
  }
  else if ( window.attachEvent ) { 
      ifrm.attachEvent( "onload", myLoad );
  }
  else {
      ifrm.onload = myLoad;
  }

  wrapper.appendChild( ifrm ); 

Above code should work in all browsers without problems.

Upvotes: 2

remy
remy

Reputation: 1501

You should add the onload callback before you set the src attribute.

Upvotes: 2

Related Questions