Path: csiph.com!weretis.net!feeder4.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: iframe function Date: Mon, 24 Oct 2016 11:58:31 +0100 Organization: PointedEars Software (PES) Lines: 59 Message-ID: <5211838.lOV4Wx5bFT@PointedEars.de> References: <16w23ipep3k6w$.1zxui9r36ciz$.dlg@40tude.net> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1477306712 23205 eJwFwYEBwCAIA7CXgNlSz1GE/09Ygo/OykVwYTDsmx08bTuWGryqEEty79gxstPvhRUyYfgBGbIQrw== (24 Oct 2016 10:58:32 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Mon, 24 Oct 2016 10:58:32 +0000 (UTC) User-Agent: KNode/4.14.2 Cancel-Lock: sha1:HW1oh2YOgWAmqSAyVtSc7Rqyj5w= X-NNTP-Posting-Host: eJwFwQkBwDAIA0BLfAnYaTviX8LukHS+LoIFQaaxQbaP7Y2gnwy4yF52rbJ0e1/kd2Rz8wcAXBCg X-User-ID: eJwFwYEBwDAEBMCVivxjHCT2H6F3MArHD8GDxd6rq70xVkGpV1qd3QLO984g77gvPcK0DJ4c6ykAVM+UH3jqFaI= Xref: csiph.com comp.lang.javascript:31606 JJ wrote: > On Mon, 24 Oct 2016 14:03:40 +1100, Andrew Poulos wrote: >> If a page contains an IFRAME and the IFRAME has some JavaScript that >> looks like: >> >> var aVar = "hello"; >> >> var myFunc = function() { >> return (aVar == "hello"); >> }; >> >> window.parent.myFunc = myFunc; >> >> and in the parent window I call myFunc() is the aVar value the one in >> the IFRAME or the one in the parent window? >> >> I've got a, for me, complex elearning course that uses an IFRAME for >> some things and the parent window for others and I'm trying to debug >> some errors but I don't really understand what the "correct" answer >> should be. > > First of all, if the IFRAME's document is in a different host, or using > different protocol and/or port number, the code that assign a variable > onto the parent window will fail due to cross scripting violation. There are *several* possible reasons why after the assignment window.parent.myFunc !== myFunc; the Same Origin Policy, which you attempted to describe, is one of them. > Otherwise, the aVar checked by myFunc would be the one in the IFRAME's > document. Incorrect. > The reason is that the myFunc() in the parent window is only a > reference to myFunc() in the IFRAME. The *value* of window.parent.myFunc is a reference to an object _in heap memory_. > The function itself is still in the IFRAME. The function still is that object stored _in heap memory_. “myFunc“ is a variable whose value is referring that object. > Thus, it only sees the aVar variable in the IFRAME. The function referred to by the value of “myFunc” compares against the value of the variable “aVar” declared in the iframe document because when the former was created it was *bound* to the latter (see: “closure” in general, and “lexical contexts” in the ECMAScript Language Specification, from the 2015 Edition, in particular). -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.