简体   繁体   中英

Determine which frame (name) called a function in JavaScript

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? ie: 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 Function constructors, you may detect it like so:

function doStuff() {
    if (doStuff.caller instanceof Function) {
        // called from top
    } else if (doStuff.caller instanceof frame1.contentWindow.Function) {
        // called from frame1
    } ...
}

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.

A possible answer, pass the window object along in your arguments and your function will act based on which window was passed in. If they are named frames, the windows should have a name property.

EDIT: Alternative Solution

Here's a technique you can use if you are creating the iframes and you want to hide the requestFocus window param from the caller within the iframe:

Once you create the iframe, you plug a wrapper requestFocus into the iframe's window that passes its own window.

Therefore, when generating the content for the iframe, you can create a function like the following in each iframe:

function requestFocus() {
  parent.requestFocus(window);
}

Like I said, I don't think you can get away from passing the window object, but you can at least hide that from the caller. Also, just because you didn't write the API, doesn't mean you shouldn't have any input. What you are asking for (automagically determining which frame called me) is just not supported by JS and sounds like bad design in the first place

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM