find largest frame

J

Jack

Hello,

What would be the easiest way to find the largest displayed document
within a frameset? Here are the steps I would like to take in
designing the algorithm:

1. Attempt to find a top level 'body' element of XHTMl document. If
found, return element.
2. No 'body' found, so attempt to find a 'frameset'.
3. Found 'frameset', so now find largest frame within 'frameset'.
4. Found largest frame within frameset, return 'body' element of
frame.
5. If largest frame contained no 'body' element repeast step 2,
continue until 'body' element found.

Problem:

1. What may appear to be the largest frame within a frameset, may in
fact be the smallest, if it contains nested framsets.

2. Seems messy.
 
P

pr

Jack said:
What would be the easiest way to find the largest displayed document
within a frameset? Here are the steps I would like to take in
designing the algorithm:

1. Attempt to find a top level 'body' element of XHTMl document. If
found, return element.
2. No 'body' found, so attempt to find a 'frameset'.
3. Found 'frameset', so now find largest frame within 'frameset'.
4. Found largest frame within frameset, return 'body' element of
frame.
5. If largest frame contained no 'body' element repeast step 2,
continue until 'body' element found.

You're confusing HTML elements like <frame> with browser objects like
window. It might just about be possible to navigate frames using the DOM
but nobody in their right mind would try it.
Problem:

1. What may appear to be the largest frame within a frameset, may in
fact be the smallest, if it contains nested framsets.

2. Seems messy.

Here is a list of ingredients you want:

window.top
window.frames: both window.frames.length and window.frames
recursion

Once you have an array of window objects, you can go about the business
of comparing them, possibly using window.innerWidth/Height, but see a
recent thread about getting window sizes.
 
J

Jack

Jack said:
What would be the easiest way to find the largest displayed document
within a frameset? Here are the steps I would like to take in
designing the algorithm:
1. Attempt to find a top level 'body' element of XHTMl document. If
found, return element.
2. No 'body' found, so attempt to find a 'frameset'.
3. Found 'frameset', so now find largest frame within 'frameset'.
4. Found largest frame within frameset, return 'body' element of
frame.
5. If largest frame contained no 'body' element repeast step 2,
continue until 'body' element found.

You're confusing HTML elements like <frame> with browser objects like
window. It might just about be possible to navigate frames using the DOM
but nobody in their right mind would try it.
1. What may appear to be the largest frame within a frameset, may in
fact be the smallest, if it contains nested framsets.
2. Seems messy.

Here is a list of ingredients you want:

window.top
window.frames: both window.frames.length and window.frames
recursion

Once you have an array of window objects, you can go about the business
of comparing them, possibly using window.innerWidth/Height, but see a
recent thread about getting window sizes.


Hello,

You've given me a few methods and attributes which will allow me to
compare values, which is useful. But how does this address my problem
of:

'1. What may appear to be the largest frame within a frameset, may in
fact be the smallest, if it contains nested framsets'.

Sure I could use the methods / attributes you have provided to
recursively descend framesets, but then I can imagine a horrible mess
of trying to detect when the frame I'm currently (recursively) in gets
smaller than a previous top level frame.

Jack.
 
J

Jack

You're confusing HTML elements like <frame> with browser objects like
window. It might just about be possible to navigate frames using the DOM
but nobody in their right mind would try it.
Here is a list of ingredients you want:
window.top
window.frames: both window.frames.length and window.frames
recursion

Once you have an array of window objects, you can go about the business
of comparing them, possibly using window.innerWidth/Height, but see a
recent thread about getting window sizes.

Hello,

You've given me a few methods and attributes which will allow me to
compare values, which is useful. But how does this address my problem
of:

'1. What may appear to be the largest frame within a frameset, may in
fact be the smallest, if it contains nested framsets'.

Sure I could use the methods / attributes you have provided to
recursively descend framesets, but then I can imagine a horrible mess
of trying to detect when the frame I'm currently (recursively) in gets
smaller than a previous top level frame.

Jack.


In and attempt to clarify the problem, imagine:

A web page with a frameset called fsA. fsA is made up up two frames,
called f1 and f2. Using JavaScript I detect that frame f1 is wider
than frame f2. I descend into frame f1 and find that it contains a
frameset fsB containing two frames f3 and f4. Here it is possible
that frames f3 and f4 are both less wide than unvisited frame f2 of
frameset fsA. In order to know this I would have to have stored the
width of frame f2, even though I was not descending into it. In other
words I would have to remember the width of all frames as I
recursively descend into framesets. The point at which I am still
recursively descending AND the frame is less wide than a previously
unvisited frame, I should bail out and commence descending into
previously unvisited frame to see if I can't find a wider frame.

This is extremely messy and would be difficult to implement.
 
P

pr

Jack said:
Jack said:
What would be the easiest way to find the largest displayed document
within a frameset?
[...]
Here is a list of ingredients you want:

window.top
window.frames: both window.frames.length and window.frames
recursion

Once you have an array of window objects, you can go about the business
of comparing them, possibly using window.innerWidth/Height, but see a
recent thread about getting window sizes.


Hello,

You've given me a few methods and attributes which will allow me to
compare values, which is useful. But how does this address my problem
of:

'1. What may appear to be the largest frame within a frameset, may in
fact be the smallest, if it contains nested framsets'.

Sure I could use the methods / attributes you have provided to
recursively descend framesets, but then I can imagine a horrible mess
of trying to detect when the frame I'm currently (recursively) in gets
smaller than a previous top level frame.


I think you're being pessimistic. The simple code below will show a
pretty-printed list of current frame names and sizes (so long as all the
content comes from the same domain). Maybe that'll make things clearer.

var tabs = "\n\t\t\t\t\t\t\t";

function getFrames(w, level) {
var f = w.frames, l = w.frames.length;
var indent = tabs.substring(0, level), i, fm, s= "";

for (i = 0; i < l; i++) {
fm = f;
s += indent + " frame name " + fm.name;
s += getFrames(fm, level +1);
}

if (!l) {
s += indent + "w: " + w.innerWidth + " h: " + w.innerHeight + "
(" + (w.innerWidth * w.innerHeight) + "px)";
}

return s;
}

alert(getFrames(window.top, 1));
 
D

David Mark

Jack said:
Jack wrote:
What would be the easiest way to find the largest displayed document
within a frameset?
[...]
Here is a list of ingredients you want:
window.top
window.frames: both window.frames.length and window.frames
recursion
Once you have an array of window objects, you can go about the business
of comparing them, possibly using window.innerWidth/Height, but see a
recent thread about getting window sizes.

You've given me a few methods and attributes which will allow me to
compare values, which is useful. But how does this address my problem
of:

'1. What may appear to be the largest frame within a frameset, may in
fact be the smallest, if it contains nested framsets'.
Sure I could use the methods / attributes you have provided to
recursively descend framesets, but then I can imagine a horrible mess
of trying to detect when the frame I'm currently (recursively) in gets
smaller than a previous top level frame.

I think you're being pessimistic. The simple code below will show a
pretty-printed list of current frame names and sizes (so long as all the
content comes from the same domain). Maybe that'll make things clearer.

var tabs = "\n\t\t\t\t\t\t\t";

function getFrames(w, level) {
var f = w.frames, l = w.frames.length;
var indent = tabs.substring(0, level), i, fm, s= "";

for (i = 0; i < l; i++) {
fm = f;
s += indent + " frame name " + fm.name;
s += getFrames(fm, level +1);
}

if (!l) {
s += indent + "w: " + w.innerWidth + " h: " + w.innerHeight + "
(" + (w.innerWidth * w.innerHeight) + "px)";
}

return s;
}

alert(getFrames(window.top, 1));- Hide quoted text -


Of course, that won't do anything useful in IE, which doesn't support
innerWidth/Height properties.

This is untested with a frameset, though I did simulate one with a
couple of nested IFrames.

var windowArea = (function() {
if (typeof(this.innerHeight) == 'number') {
return function(w) { return w.innerHeight * w.innerWidth; };
}
return function(w) {
var doc = w.document;
var prop = (doc.documentElement && doc.compatMode &&
doc.compatMode.indexOf('CSS') != -1)?'documentElement':'body';
return doc[prop].clientHeight * doc[prop].clientWidth;
};
})();

function maxFrame(w) {
var obj, frm, max = 0, f = w.frames, l = f.length;

if (!l) {
return {win:w, size:windowArea(w)};
}
while (l) {
obj = maxFrame(f[--l]);
if (obj.size > max) {
max = obj.size;
frm = obj.win;
}
}
return {win:frm, size:max};
}

function getBody(w) {
var doc = w.document;
return doc.body || (doc.getElementsByTagName &&
doc.getElementsByTagName('body')[0]);
}

function maxWindow(w) {
var body = getBody(w);
return (body)?w:maxFrame(w).win;
}

function maxBody(w) {
return getBody(maxWindow(w));
}
 
J

Jack

Jack wrote:
Jack wrote:
What would be the easiest way to find the largest displayed document
within a frameset?
[...]
Here is a list of ingredients you want:
window.top
window.frames: both window.frames.length and window.frames
recursion
Once you have an array of window objects, you can go about the business
of comparing them, possibly using window.innerWidth/Height, but see a
recent thread about getting window sizes.
Hello,
You've given me a few methods and attributes which will allow me to
compare values, which is useful. But how does this address my problem
of:
'1. What may appear to be the largestframewithin a frameset, may in
fact be the smallest, if it contains nested framsets'.
Sure I could use the methods / attributes you have provided to
recursively descend framesets, but then I can imagine a horrible mess
of trying to detect when theframeI'm currently (recursively) in gets
smaller than a previous top levelframe.
I think you're being pessimistic. The simple code below will show a
pretty-printed list of currentframenames and sizes (so long as all the
content comes from the same domain). Maybe that'll make things clearer.
var tabs = "\n\t\t\t\t\t\t\t";
function getFrames(w, level) {
var f = w.frames, l = w.frames.length;
var indent = tabs.substring(0, level), i, fm, s= "";
for (i = 0; i < l; i++) {
fm = f;
s += indent + "framename " + fm.name;
s += getFrames(fm, level +1);
}
if (!l) {
s += indent + "w: " + w.innerWidth + " h: " + w.innerHeight + "
(" + (w.innerWidth * w.innerHeight) + "px)";
}
return s;
}
alert(getFrames(window.top, 1));- Hide quoted text -

Of course, that won't do anything useful in IE, which doesn't support
innerWidth/Height properties.
This is untested with a frameset, though I did simulate one with a
couple of nested IFrames.
var windowArea = (function() {
if (typeof(this.innerHeight) == 'number') {
return function(w) { return w.innerHeight * w.innerWidth; };
}
return function(w) {
var doc = w.document;
var prop = (doc.documentElement && doc.compatMode &&
doc.compatMode.indexOf('CSS') != -1)?'documentElement':'body';
return doc[prop].clientHeight * doc[prop].clientWidth;
};
})();
function maxFrame(w) {
var obj, frm, max = 0, f = w.frames, l = f.length;
if (!l) {
return {win:w, size:windowArea(w)};
}
while (l) {
obj = maxFrame(f[--l]);
if (obj.size > max) {
max = obj.size;
frm = obj.win;
}
}
return {win:frm, size:max};
}
function getBody(w) {
var doc = w.document;
return doc.body || (doc.getElementsByTagName &&
doc.getElementsByTagName('body')[0]);
}
function maxWindow(w) {
var body = getBody(w);
return (body)?w:maxFrame(w).win;
}
function maxBody(w) {
return getBody(maxWindow(w));
}

Jack wrote:
Jack wrote:
What would be the easiest way to find the largest displayed document
within a frameset?
[...]
Here is a list of ingredients you want:
window.top
window.frames: both window.frames.length and window.frames
recursion
Once you have an array of window objects, you can go about the business
of comparing them, possibly using window.innerWidth/Height, but see a
recent thread about getting window sizes.
Hello,
You've given me a few methods and attributes which will allow me to
compare values, which is useful. But how does this address my problem
of:
'1. What may appear to be the largestframewithin a frameset, may in
fact be the smallest, if it contains nested framsets'.
Sure I could use the methods / attributes you have provided to
recursively descend framesets, but then I can imagine a horrible mess
of trying to detect when theframeI'm currently (recursively) in gets
smaller than a previous top levelframe.
I think you're being pessimistic. The simple code below will show a
pretty-printed list of currentframenames and sizes (so long as all the
content comes from the same domain). Maybe that'll make things clearer.
var tabs = "\n\t\t\t\t\t\t\t";
function getFrames(w, level) {
var f = w.frames, l = w.frames.length;
var indent = tabs.substring(0, level), i, fm, s= "";
for (i = 0; i < l; i++) {
fm = f;
s += indent + "framename " + fm.name;
s += getFrames(fm, level +1);
}
if (!l) {
s += indent + "w: " + w.innerWidth + " h: " + w.innerHeight + "
(" + (w.innerWidth * w.innerHeight) + "px)";
}
return s;
}
alert(getFrames(window.top, 1));- Hide quoted text -

Of course, that won't do anything useful in IE, which doesn't support
innerWidth/Height properties.
This is untested with a frameset, though I did simulate one with a
couple of nested IFrames.
var windowArea = (function() {
if (typeof(this.innerHeight) == 'number') {
return function(w) { return w.innerHeight * w.innerWidth; };
}
return function(w) {
var doc = w.document;
var prop = (doc.documentElement && doc.compatMode &&
doc.compatMode.indexOf('CSS') != -1)?'documentElement':'body';
return doc[prop].clientHeight * doc[prop].clientWidth;
};
})();
function maxFrame(w) {
var obj, frm, max = 0, f = w.frames, l = f.length;
if (!l) {
return {win:w, size:windowArea(w)};
}
while (l) {
obj = maxFrame(f[--l]);
if (obj.size > max) {
max = obj.size;
frm = obj.win;
}
}
return {win:frm, size:max};
}
function getBody(w) {
var doc = w.document;
return doc.body || (doc.getElementsByTagName &&
doc.getElementsByTagName('body')[0]);
}
function maxWindow(w) {
var body = getBody(w);
return (body)?w:maxFrame(w).win;
}
function maxBody(w) {
return getBody(maxWindow(w));
}

Well thank you both, extremely nice examples and very helpful. I am
in fact primarily concerned with Firefox, although may at a later date
want to incorporate IE. I have the idea now that it would be better
to first build up a data structure, perhaps an array, of objects that
model the data I require, for example I could have an array of myFrame
objects, built up using the methods posted here:

var myFrame =
{
friendlyName : "",
reference : null,
width : -1,Q
height : -1;

}

Once I had this data structure I could search for the widestframe.
Perhaps even caching the result for quicker reference in the future.
IN fact I could do many useful things with the data (sort etc). Does
this sound sensible?

Thank you both. I will try and post my results and solution here.


I have written a small script, pretty much just using the function
getFrames, posted by pr. However, in order for this script to work I
have to place it in the inner-most frame. Presumably this is because
it is at this point all frames have been loaded? Is there a way to
run the script from the top level page (containing the top most
framset)?

Many thanks,

Jack
 
P

pr

Jack said:
[big snip]

Please only quote the part you're replying to.
I have written a small script, pretty much just using the function
getFrames, posted by pr. However, in order for this script to work I
have to place it in the inner-most frame. Presumably this is because
it is at this point all frames have been loaded? Is there a way to
run the script from the top level page (containing the top most
framset)?

If you're running the script close to when the frameset(s) load(s), then
you might have an issue with partially-downloaded content and it's
probably only an accident that it appears to work in the innermost page.
If you describe the symptoms and your frameset structure, I can look
into it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top