Get rid of the IE iframe “click”

Written by on November 12th, 2007 in Ajax News.

I am sure you have run into this before. You are on a site, interacting with a single page app, and then you hit the back button and IE clicks at you. The navigation “click” happens with iframes, and be a nuisance if you are trying to use iframes in a non-navigational way.

Julien Lecomte has a work around to by pass it by replacing DOM nodes with new iframes and such:

JAVASCRIPT:

  1.  
  2. function setIFrameSrc(iframe, src) {
  3.     var el;
  4.     iframe = YAHOO.util.Dom.get(iframe);
  5.     if (YAHOO.env.ua.ie) {
  6.         // Create a new hidden iframe.
  7.         el = iframe.cloneNode(true);
  8.         el.style.position = “absolute”;
  9.         el.style.visibility = “hidden”;
  10.         // keep the original iframe id unique!
  11.         el.id = “”;
  12.         // Listen for the onload event.
  13.         YAHOO.util.Event.addListener(el, “load”, function () {
  14.             // First, remove the event listener or the old iframe
  15.             // we intend to discard will not be freed…
  16.             YAHOO.util.Event.removeListener(this, “load”, arguments.callee);
  17.             // Show the iframe.
  18.             this.style.position = “”;
  19.             this.style.visibility = “”;
  20.             // Replace the old iframe with the new one.
  21.             iframe.parentNode.replaceChild(this, iframe);
  22.             // Reset the iframe id.
  23.             this.id = iframe.id;
  24.         });
  25.         // Set its src first…
  26.         el.src = src;
  27.         // …and then append it to the body of the document.
  28.         document.body.appendChild(el);
  29.     } else {
  30.         iframe.src = src;
  31.     }
  32. }
  33.  

I loved Julien’s note:

This technique is used by the new Yahoo! Mail (which used to sound like an automatic rifle…:0)

Source: Ajaxian
Original Article: http://feeds.feedburner.com/~r/ajaxian/~3/183544425/get-rid-of-the-ie-iframe-click

Leave a Reply

You must be logged in to post a comment.



Site Navigation