Change the execution context from parent to child frame

I'm not even sure if this is possible, but is there a way to set the execution context without setting a value for "this"?

Basically, I mean: code from one frame is executed in the context of another frame, so when I access global objects (for example: window, document ...) from the function defined in frame1, It will be executed in frame2 environment.

If this is not possible, some workarounds? Please do not say “just define the function in the child frame”, I am dealing with a larger application infrastructure, and it would be pointless and inefficient if I had to load two instances of the whole structure.

EDIT: Here is some code that should demonstrate what I'm trying to do. At startup, a warning should appear that if a solution is found, "iframe.html" is displayed at the end of the location line.

<script> function run() { go.call(window.iframe); } function go() { alert(window.location); } </script> <iframe src="iframe.html" name="iframe" onload="run()"> 

Thanks.

+4
source share
4 answers

If you can “break” your “integration” code in closing with window aliases to window.iframe , you can achieve what you want:

 (function(window) { // your integration code... // the whole code you want to frame... alert(window.location); })(window.iframe); 

But you need to “create” all the code you want to interact with.

In addition, you can set the "integration" functions to call "outside" by passing other "context" objects to:

 var context = {}; (function(window, context) { // your integration code... context.f = function() { ... }; // the whole code you want to frame... alert(window.location); })(window.iframe, context); context.f(); 
+1
source

In this case, the obsolete with statement is used, as well as eval, but this may be the only way to accomplish what you want.

 function run() { with (window.iframe) { eval("(" + go.toString() + ")()"); } } function go() { alert(window.location); } 
+2
source

Use Function.apply and Function.call .

 function foo(x, y, z) { console.log(this === someFrame); // true } foo.call(someFrame, 1, 2, 3); 

EDIT Based on your sample code and comments below, the answer is no. Scripts cannot change the global scope.

0
source

Include the code in both frames. Then call the function in another frame. Suppose there is a top-level global function called doWork() .

You enter the button in frame2, and you want to execute the function in frame1 (where frame1 is the window object representing frame1). Assuming that they are in the same domain and pass tests with the same origin , you can simply go to frame1 and call doWork() in this frame with:

 frame1.doWork(); 

This works because all global javascript global variables and functions are window object properties for this browser window / frame. Each window / frame has its own set of top-level properties. If you are in the same domain and can get the corresponding window object, you can simply call javascript in that window. Javascript must be present in this window. You cannot run javascript from your window in the contents of another window, but if you put the code in both windows, you can execute it in any of the windows, starting with the right window object.

How you get the right window object for a particular frame depends on how your frames are structured.

Assuming again that these frames are in the same domain and pass the usual tests with the same origin , you can also write the javascript function in frame2, which works with the contents of frame1. For example, you can get this code in frame2:

 frame1.document.getElementById("foo").value = ""; 

So you can write a function in frame2 that will work with frame1. However, you cannot just execute a function from frame2 in the contents of frame1 without writing it to know about working in another frame.

For example, you can write a function that will work with the desired window:

 function clearValue(id, win) { win.document.getElementById(id).value = ""; } 
-1
source

Source: https://habr.com/ru/post/1381230/


All Articles