By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

Function to call a function in all child frames

P: n/a
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[i])) {
window.frames[i].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?

Oct 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ne**@chthonic.f9.co.uk wrote:
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?
You want the apply() method for Function instance:.
http://phrogz.net/ObjJob/method.asp?id=781

Oct 16 '06 #2

P: n/a
Phrogz wrote:
ne**@chthonic.f9.co.uk wrote:
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?

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

Oct 16 '06 #3

P: n/a

Phrogz wrote:
Phrogz wrote:
ne**@chthonic.f9.co.uk wrote:
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?
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

Oct 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.