iframe - Javascript alternative for this (IE) -
i have following code:
for(var i=0;i<4;i++) { frm = document.createelement("iframe"); frm.width = "100"; frm.height = "100"; frm.src = "source.php"; frm.id = "something"; frm.attachevent("load", function() { alert(this.id); //here need alternative this.id }); var framediv = document.getelementbyid("framediv"); framediv.appendchild(frm); }
it's simplified, basicly creates 4 iframes inside div , need fire actions based on id of each frame, when it's loaded. works well, problem in ie. ie doesn't know operator this, can't access id of frame inside. there alternative can use ie?
thanks help!
abhijit: actually, whole code this:
if(frm.addeventlistener) { frm.addeventlistener("load", function() { alert(this.id); },false); } else if(frm.attachevent) { frm.attachevent("onload", function() { alert(this.id); }); } else { frm.onload=function() { alert(this.id); }; }
so should works in browsers.
i'm not sure why wouldn't work in ie, changing
frm.attachevent("load", function() { alert(this.id); });
to
// wrap in anonymous function fix scope issues (function(frm) { frm.attachevent("load", function() { alert(frm.id); }); })(frm);
should give consistent reference iframe.
(edit: updated above code fix scope issues within loop. why should avoid creating closures in loop - frm
references point last defined iframe unless take explicit measures fix scope.)
Comments
Post a Comment