Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #19759 > unrolled thread

Find an object in a window

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2013-05-16 14:49 +1000
Last post2013-05-19 10:06 +1000
Articles 5 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  Find an object in a window Andrew Poulos <ap_prog@hotmail.com> - 2013-05-16 14:49 +1000
    Re: Find an object in a window Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-16 11:12 +0200
      Re: Find an object in a window Andrew Poulos <ap_prog@hotmail.com> - 2013-05-16 22:12 +1000
        Re: Find an object in a window Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-18 16:25 +0200
          Re: Find an object in a window Andrew Poulos <ap_prog@hotmail.com> - 2013-05-19 10:06 +1000

#19759 — Find an object in a window

FromAndrew Poulos <ap_prog@hotmail.com>
Date2013-05-16 14:49 +1000
SubjectFind an object in a window
Message-ID<G4WdnS1OUt3I-AnMnZ2dnUVZ_vadnZ2d@westnet.com.au>
I'm working with some Learning Management Systems (LMS) that provide 
elearning to students. The communication between the LMS and the 
elearning is down via a js object the LMS call "API".

Now I've know that some LMS will open the elearning in a new window and 
embed API in a "hidden" iframe in the window. Some LMS will open the 
elearning in a new window and embed API in the window's opener. Some LMS 
will open the elearning in a frame within a frameset and embed API in a 
"hidden" frame.

A quick look online shows me that other LMS may also embed API in other 
locations.

I tried writing a recursive function (that sits in the elearning page) 
that searched all frames in the elearning's top window and in the top 
window of any opener which appears to work:

function findAPI(win) {
   if (win.API) {
     return win.API;
   }
	
   for (var i = 0; i < win.frames.length; i++) {
     if (win.frames[i].API) {
       return findAPI(win.frames[i]);
     }
   }
	
   if (win.opener && !win.opener.closed) {
     return findAPI(win.opener.top);
   }
}

function fLook() {
   var foundApi = findAPI(window.top);
   window.alert(foundApi);
}

Any ideas on how to make this (more) robust? For example, its not 
checking nested frames.

Andrew Poulos

[toc] | [next] | [standalone]


#19761

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-05-16 11:12 +0200
Message-ID<2083966.lBPHQm9WGc@PointedEars.de>
In reply to#19759
Andrew Poulos wrote:

> I tried writing a recursive function (that sits in the elearning page)
> that searched all frames in the elearning's top window and in the top
> window of any opener which appears to work:
> 
> function findAPI(win) {
>    if (win.API) {
>      return win.API;
>    }
> 
>    for (var i = 0; i < win.frames.length; i++) {
       var api = findAPI(win.frames[i]);
       if (api) {
         return api;
>      }
>    }
> 
>    if (win.opener && !win.opener.closed) {
>      return findAPI(win.opener.top);
>    }
> }
> 
> function fLook() {
>    var foundApi = findAPI(window.top);
>    window.alert(foundApi);
> }
> 
> Any ideas on how to make this (more) robust? For example, its not
> checking nested frames.

Now it does.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#19764

FromAndrew Poulos <ap_prog@hotmail.com>
Date2013-05-16 22:12 +1000
Message-ID<p62dneXrfKXaUAnMnZ2dnUVZ_qCdnZ2d@westnet.com.au>
In reply to#19761
On 16/05/2013 7:12 PM, Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
>
>> I tried writing a recursive function (that sits in the elearning page)
>> that searched all frames in the elearning's top window and in the top
>> window of any opener which appears to work:
>>
>> function findAPI(win) {
>>     if (win.API) {
>>       return win.API;
>>     }
>>
>>     for (var i = 0; i < win.frames.length; i++) {
>         var api = findAPI(win.frames[i]);
>         if (api) {
>           return api;
>>       }
>>     }
>>
>>     if (win.opener && !win.opener.closed) {
>>       return findAPI(win.opener.top);
>>     }
>> }
>>
>> function fLook() {
>>     var foundApi = findAPI(window.top);
>>     window.alert(foundApi);
>> }
>>
>> Any ideas on how to make this (more) robust? For example, its not
>> checking nested frames.
>
> Now it does.

Too easy! Thanks for the help.

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#19813

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-05-18 16:25 +0200
Message-ID<31855897.f2HLQ4rEVA@PointedEars.de>
In reply to#19764
Andrew Poulos wrote:

> On 16/05/2013 7:12 PM, Thomas 'PointedEars' Lahn wrote:
>> Andrew Poulos wrote:
>>> I tried writing a recursive function (that sits in the elearning page)
>>> that searched all frames in the elearning's top window and in the top
>>> window of any opener which appears to work:
>>>
>>> function findAPI(win) {
>>>     if (win.API) {
>>>       return win.API;
>>>     }
>>>
>>>     for (var i = 0; i < win.frames.length; i++) {
>>         var api = findAPI(win.frames[i]);
>>         if (api) {
>>           return api;
>>>       }
>>>     }
>>>
>>>     if (win.opener && !win.opener.closed) {
>>>       return findAPI(win.opener.top);
>>>     }
>>> }
>>>
>>> function fLook() {
>>>     var foundApi = findAPI(window.top);
>>>     window.alert(foundApi);
>>> }
>>>
>>> Any ideas on how to make this (more) robust? For example, its not
>>> checking nested frames.
>>
>> Now it does.
> 
> Too easy! Thanks for the help.

You're welcome.  An omission and a further suggestion on my part, though.

The omission:

  for (var i = 0, len = win.frames.length; i < len; i++)

is more efficient than

  for (var i = 0; i < win.frames.length; i++)

and as safe if you can be sure that the number of items does not change 
while looping.  For that matter, if you stored the value of “win.frames” in 
a variable and access that variable instead, it would be even more 
efficient:

  for (var i = 0, frames = win.frames, len = frames.length;
       i < len; i++)
  {
    var api = findAPI(frames[i]);
    if (api) {
      return api;
    }
  }

Or you might want to wrap it as suggested e. g. in 
<http://naildrivin5.com/blog/2013/05/17/source-code-typography.html?utm_content=buffer5f552&utm_source=buffer&utm_medium=twitter&utm_campaign=Buffer> 
(from David Bryant Copeland [@davetron5000 at Github], by tweet of 
@kevinSuttle):

  for (var i = 0,
           frames = win.frames,
           len = frames.length;
       i < len; i++)
  {
    var api = findAPI(frames[i]);
    if (api) {
      return api;
    }
  }


The suggestion: _it's_ _e-learning_ :)

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#19816

FromAndrew Poulos <ap_prog@hotmail.com>
Date2013-05-19 10:06 +1000
Message-ID<D6qdnWHdd7UsigXMnZ2dnUVZ_rednZ2d@westnet.com.au>
In reply to#19813
On 19/05/2013 12:25 AM, Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
>
>> On 16/05/2013 7:12 PM, Thomas 'PointedEars' Lahn wrote:
>>> Andrew Poulos wrote:
>>>> I tried writing a recursive function (that sits in the elearning page)
>>>> that searched all frames in the elearning's top window and in the top
>>>> window of any opener which appears to work:
>>>>
>>>> function findAPI(win) {
>>>>      if (win.API) {
>>>>        return win.API;
>>>>      }
>>>>
>>>>      for (var i = 0; i < win.frames.length; i++) {
>>>          var api = findAPI(win.frames[i]);
>>>          if (api) {
>>>            return api;
>>>>        }
>>>>      }
>>>>
>>>>      if (win.opener && !win.opener.closed) {
>>>>        return findAPI(win.opener.top);
>>>>      }
>>>> }
>>>>
>>>> function fLook() {
>>>>      var foundApi = findAPI(window.top);
>>>>      window.alert(foundApi);
>>>> }
>>>>
>>>> Any ideas on how to make this (more) robust? For example, its not
>>>> checking nested frames.
>>>
>>> Now it does.
>>
>> Too easy! Thanks for the help.
>
> You're welcome.  An omission and a further suggestion on my part, though.
>
> The omission:
>
>    for (var i = 0, len = win.frames.length; i < len; i++)
>
> is more efficient than
>
>    for (var i = 0; i < win.frames.length; i++)
>
> and as safe if you can be sure that the number of items does not change
> while looping.  For that matter, if you stored the value of “win.frames” in
> a variable and access that variable instead, it would be even more
> efficient:
>
>    for (var i = 0, frames = win.frames, len = frames.length;
>         i < len; i++)
>    {
>      var api = findAPI(frames[i]);
>      if (api) {
>        return api;
>      }
>    }
>
> Or you might want to wrap it as suggested e. g. in
> <http://naildrivin5.com/blog/2013/05/17/source-code-typography.html?utm_content=buffer5f552&utm_source=buffer&utm_medium=twitter&utm_campaign=Buffer>
> (from David Bryant Copeland [@davetron5000 at Github], by tweet of
> @kevinSuttle):
>
>    for (var i = 0,
>             frames = win.frames,
>             len = frames.length;
>         i < len; i++)
>    {
>      var api = findAPI(frames[i]);
>      if (api) {
>        return api;
>      }
>    }
>
>
> The suggestion: _it's_ _e-learning_ :)

If the number of frames is, say, 10 or less would it make a noticeable 
difference from the first way or are you promoting a generally better 
approach?

Andrew Poulos

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web