Before the question, let me preface this with the fact that I am working on a very atypical project. I am not concerned with security or cross-browser implications. I am using a custom build of the Chromium project.
My question is, if I call a function from a different namespace, how can I determine the originating namespace?
For instance, lets say I have three frames:
1. window
-> 2. frame1 //is a child of window
-> 3. frame2 //is a child of window
Now, lets say that some JavaScript within window has a function called doStuff(), that I give frame1 and frame2 access to by going:
var localDoStuff = top.doStuff;
Within the function doStuff, is there a way for me to determine which of frame1 and frame2 called it? i.e: If I call localDoStuff within the scope of frame1, can I get frame1‘s DOM window object within the scope of doStuff?
If I use arguments.callee.caller, it gives me the string representation of the function that called it. However, I want to get the scope in which that function was defined.
I do have access to the source of the browser if anyone has a good, directed idea of how to modify the browser in order to allow this.
Pasted from comments:
I am basically emulating an OS that runs multiple applications. Each of these applications is an iframe that has access to an API defined in the parent frame of the applications. Let’s say this API had the function requestFocus() — no arguments allowed, as defined in the API spec that I am implementing. In order to give the right iframe focus, I would have to know which iframe called the requestFocus function. I know that this is not an ideal situation — I didn’t write the API spec, unfortunately…
Thanks for your help, I know that this is a really weird question.
Since different frames have different
Functionconstructors, you may detect it like so:However, this does not work if the function is called from top-level code in which case
doStuff.caller === null.I’m really curious to see if anyone has a solution for that.