Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 603
jabal Avatar asked Mar 05 '26 07:03

jabal


1 Answers

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.

like image 124
emre nevayeshirazi Avatar answered Mar 07 '26 19:03

emre nevayeshirazi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!