Function to call a function in all child frames

N

news

I have a document that can contain any number of iframes which have
further copies of the same document (and so on). In practice, we
shouldn't ever have frames within frames, but I'd like to make the
implementation a general case.

Various of the functions in the parent document need to call themselves
in the child documents with the same parameters as they've just been
passed.



Now, I can stick a loop in each function that goes throught the
window.frames array and calls the function for each frame, but for I'd
like to write a function to do this.

It's at this point that my brain explodes. If I limit it to a single
parameter we can do:

function functionExists(funcName, win) {
win = win?win:window;
return (win[funcName] && typeof win[funcName] == 'function');
}

function forEachChild(funcName, param) {
if (functionExists(funcName)) {
window[funcName](param);
if (window.frames && window.frames.length) {
for(var i = 0, L = window.frames.length; i<L; i++) {
if (functionExists('forEachChild', window.frames)) {
window.frames.forEachChild(func, param);
}
}
}
}
}

But how do I expand that to any number of parameters? Passing functions
by their name rather than as an object seems a bit naff too. Any ideas?
 
N

news

Phrogz said:
Phrogz said:
You want the apply() method for Function instance:
http://phrogz.net/ObjJob/method.asp?id=781

Oops, sent that too soon. For example:

function f( a, b, c ){
return [ a, b, c ].join( "--" );
}

function g( ){
return f.apply( null, arguments );
}

alert( g( 1,4,5 ) );
// 1--4--5

And to think I was only looking at the .call() and .apply() methods the
other day and wondering when they'd be useful. Nice.

Still doesn't get around the having to pass the function name as a
string, but that's inevitable because the functions to be called are
all different objects that happen to have the same name (and the same
source code).

Thanks for pointing me in the right direction
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top