Wednesday, July 14, 2010

Comparing window references

or "Evil twins" === vs. == in cross-windows communication

Comparing window references or "Evil twins" === vs. == in cross-windows communication.

As you may have read in meany sources
=== is preffered over ==
Mr.D calls the == variant The evil twin, but... MSIE (again) may surprise you:


Lets have two windows A and B

now in A lets call this wierd construction:

// in window A, "ask window B to create object"
new B.Object1();

// code inside window B looks like this
function Object1(p1,p2,p3)
{
this.p1=p1;
this.p2=p2;
this.p3=p3;
//what DOES window mean:,
// a) is it wnd from "new wnd.Object1()" or
// b) is it callers window context ?
// see test case for answer ;-)
this._ctx=window;
}
Object1.prototype.getCtx=function()
{
return this._ctx;
}


This construct will work
in all browsers
in all types of windows (iFrame, window.open, modal, modeless dialogs)
but and WILL NOT WORK in MSIE modelessDialog and window.open.
and the call will end up with "Invalid procedure call or argument".

However in those working in explorer (iframe and tweaked modal dialog)
let's try to compare window references:


// remote window is opener or parent or self
// depending on the type of "popup" method used

var remote=new remoteWnd.Object1();
var remoteCtx=remote.getCtx();
asrt(remoteCtx===remoteWnd,"window refs broken ?");
return remoteCtx.location.href;



All browsers (Safari, FF, Opera, Chrome) pass this test for
all supported types of cross-referenced windows (iFrame, window.open,showModalDialog)
but

MSIE (6,7,8) fails to compare window references with ===

summary:

// does not work
remoteCtx===remoteWnd
// seems to work but what is actually compared ? (TODO: see specs.)
remoteCtx==remoteWnd
// works but means something different right ?
remoteCtx.document===remoteWnd.document


Of course there is more XB magic in cross-window communication
(MSIE and Safari identified as trouble makers)
and I may return with more notes....

No comments:

Post a Comment