f



iframe function

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.

Andrew Poulos
0
Andrew
10/24/2016 3:03:40 AM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

13 Replies
551 Views

Similar Articles

[PageSpeed] 57

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?

Yes.

<http://catb.org/esr/faqs/smart-questions.html#prune>
<http://catb.org/esr/faqs/smart-questions.html#idm45835691719280>
 
> 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.

<http://catb.org/esr/faqs/smart-questions.html#idm45835691716720>

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
0
Thomas
10/24/2016 3:26:11 AM
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.
> 
> Andrew Poulos

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.

Otherwise, the aVar checked by myFunc would be the one in the IFRAME's
document. The reason is that the myFunc() in the parent window is only a
reference to myFunc() in the IFRAME. The function itself is still in the
IFRAME. Thus, it only sees the aVar variable in the IFRAME.
0
JJ
10/24/2016 6:25:34 AM
On 24/10/2016 2:26 PM, Thomas 'PointedEars' Lahn wrote:
> 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?
>
> Yes.
>
> <http://catb.org/esr/faqs/smart-questions.html#prune>
> <http://catb.org/esr/faqs/smart-questions.html#idm45835691719280>
>
>> 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.
>
> <http://catb.org/esr/faqs/smart-questions.html#idm45835691716720>

Your understanding of JavaScript and other internet-based technologies 
is unquestioned (at least by me) but I often feel your advice is less 
than helpful.

Andrew Poulos
0
Andrew
10/24/2016 8:17:07 AM
In article <4sSdnU3AsOIeXpDFnZ2dnUU7-TXNnZ2d@westnet.com.au>, Andrew
Poulos <ap_prog@hotmail.com> wrote:

>On 24/10/2016 2:26 PM, Thomas 'PointedEars' Lahn wrote:
>> 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?
>>
>> Yes.
>>
>> <http://catb.org/esr/faqs/smart-questions.html#prune>
>> <http://catb.org/esr/faqs/smart-questions.html#idm45835691719280>
>>
>>> 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.
>>
>> <http://catb.org/esr/faqs/smart-questions.html#idm45835691716720>
>
>Your understanding of JavaScript and other internet-based technologies 
>is unquestioned (at least by me) but I often feel your advice is less 
>than helpful.

I think you mean "never helpful".

-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
0
Tim
10/24/2016 9:03:04 AM
Andrew Poulos wrote:

> Your understanding of JavaScript and other internet-based technologies
> is unquestioned (at least by me) but I often feel your advice is less
> than helpful.

As you can see from the references, the latter too often applies to your 
"questions".  Read, think, post; in that order.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
0
Thomas
10/24/2016 10:16:24 AM
In article <4984144.DvuYhMxLoT@PointedEars.de>, Thomas 'PointedEars'
Lahn <PointedEars@web.de> wrote:

>Andrew Poulos wrote:
>
>> Your understanding of JavaScript and other internet-based technologies
>> is unquestioned (at least by me) but I often feel your advice is less
>> than helpful.
>
>As you can see from the references, the latter too often applies to your 
>"questions".  Read, think, post; in that order.

A proper teacher can guess the source of confusion in someone's mind by
reading the question and applying a bit of thought. You are evidently
incapable of doing this.

-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
0
Tim
10/24/2016 10:28:44 AM
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: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
0
Thomas
10/24/2016 10:58:31 AM
Thomas 'PointedEars' Lahn wrote:

> 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).

The proper key word is “lexical _environment”; see in particular 
<http://www.ecma-international.org/ecma-262/7.0/#sec-function-definitions-runtime-semantics-evaluation> (the 7th edition, of 2016, is easier to read 
at least because of better site navigation).

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
0
Thomas
10/24/2016 11:16:48 AM
On 24-10-16 12:16, Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
>
>> Your understanding of JavaScript and other internet-based technologies
>> is unquestioned (at least by me) but I often feel your advice is less
>> than helpful.
>
> As you can see from the references, the latter too often applies to your
> "questions".  Read, think, post; in that order.
>

The order should be:
Read, think, think again, type post, think again, post

This 3rd item it to make sure you REALLY understand what is being 
questioned.....

This fifth item is too often forgotten. It should help in getting 
ananswer to "Is this answer helpfull to the person asking the question?"


0
Luuk
10/24/2016 5:19:36 PM
In comp.lang.javascript message <580e42a6$0$872$e4fe514c@news.xs4all.nl>
, Mon, 24 Oct 2016 19:19:36, Luuk <luuk@invalid.lan> posted:

>On 24-10-16 12:16, Thomas 'PointedEars' Lahn wrote:
>> Andrew Poulos wrote:
>>
>>> Your understanding of JavaScript and other internet-based technologies
>>> is unquestioned (at least by me) but I often feel your advice is less
>>> than helpful.
>>
>> As you can see from the references, the latter too often applies to your
>> "questions".  Read, think, post; in that order.
>>
>
>The order should be:
>Read, think, think again, type post, think again, post
>
>This 3rd item it to make sure you REALLY understand what is being
>questioned.....
>
>This fifth item is too often forgotten. It should help in getting
>ananswer to "Is this answer helpfull to the person asking the
>question?"


Good, but incomplete.

Before starting to write a follow-up, one should re-read the previous
article[s].  And re-read likewise after composing the answer.  Both of
those should be in "do until OK" loops.

Also, whenever reasonably feasible, any code in the answer should be
tested in another such loop.

-- 
 (c) John Stockton, Surrey, UK.  �@merlyn.demon.co.uk   Turnpike v6.05   MIME.
 Merlyn Web Site <                       > - FAQish topics, acronyms, & links.


0
Dr
10/25/2016 8:19:23 PM
On 24-10-2016 06:17, Andrew Poulos wrote:
> On 24/10/2016 2:26 PM, Thomas 'PointedEars' Lahn wrote:
>> Andrew Poulos wrote:

>
> Your understanding of JavaScript and other internet-based technologies
> is unquestioned (at least by me) but I often feel your advice is less
> than helpful.
>
> Andrew Poulos

You finally saw the light.

-- 
Joao Rodrigues
0
Joao
10/26/2016 8:15:56 AM
On 25-10-16 22:19, Dr J R Stockton wrote:
> In comp.lang.javascript message <580e42a6$0$872$e4fe514c@news.xs4all.nl>
> , Mon, 24 Oct 2016 19:19:36, Luuk <luuk@invalid.lan> posted:
>
>> On 24-10-16 12:16, Thomas 'PointedEars' Lahn wrote:
>>> Andrew Poulos wrote:
>>>
>>>> Your understanding of JavaScript and other internet-based technologies
>>>> is unquestioned (at least by me) but I often feel your advice is less
>>>> than helpful.
>>>
>>> As you can see from the references, the latter too often applies to your
>>> "questions".  Read, think, post; in that order.
>>>
>>
>> The order should be:
>> Read, think, think again, type post, think again, post
>>
>> This 3rd item it to make sure you REALLY understand what is being
>> questioned.....
>>
>> This fifth item is too often forgotten. It should help in getting
>> ananswer to "Is this answer helpfull to the person asking the
>> question?"
>
>
> Good, but incomplete.
>
> Before starting to write a follow-up, one should re-read the previous
> article[s].  And re-read likewise after composing the answer.  Both of
> those should be in "do until OK" loops.
>

that migh give an infinite loop........

;)

> Also, whenever reasonably feasible, any code in the answer should be
> tested in another such loop.
>

Thanks for your response!
0
Luuk
10/26/2016 8:30:48 PM
Joao Rodrigues wrote:

> On 24-10-2016 06:17, Andrew Poulos wrote:
>> On 24/10/2016 2:26 PM, Thomas 'PointedEars' Lahn wrote:
>>> Andrew Poulos wrote:
>> Your understanding of JavaScript and other internet-based technologies
>> is unquestioned (at least by me) but I often feel your advice is less
>> than helpful.
> 
> You finally saw the light.

No, that is yet to happen.  For you, too.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
0
Thomas
10/27/2016 9:11:19 PM
Reply: