Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法找出哪个iframe正在调用父对象';s JavaScript函数_Javascript_Html_Iframe - Fatal编程技术网

有没有办法找出哪个iframe正在调用父对象';s JavaScript函数

有没有办法找出哪个iframe正在调用父对象';s JavaScript函数,javascript,html,iframe,Javascript,Html,Iframe,这里有一个例子 parent.html <script> function printWhoCalledMe() { console.log(???); // what goes here that will identify the caller? } <iframe src="iframe1.html"></iframe> <iframe src="iframe2.html"></iframe> <script>

这里有一个例子

parent.html

<script>
function printWhoCalledMe() {
  console.log(???);  // what goes here that will identify the caller?
}
<iframe src="iframe1.html"></iframe>
<iframe src="iframe2.html"></iframe>
<script>
    function printWhoCalledMe(iframeId) {
        console.log(iframeId);  // Prints the id of the iframe that called the function
    }
</script>

但这是家长的href。

我担心您可能不得不使用这样的字符串值

function printWhoCalledMe(callerPage) {
  console.log(callerPage);  // what goes here that will identify the caller?
}
你可以从你的子框架中用这样的参数调用这个函数

function printWhoCalledMe(callerPage) {
  console.log(callerPage);  // what goes here that will identify the caller?
}
iframe1.html

<script>
window.parent.printWhoCalledMe();
</script>
<script>
window.parent.printWhoCalledMe("iframe1");
</script>
<script>
    window.parent.printWhoCalledMe(window.frameElement.id);
</script>

window.parent.printWhoCalledMe(“iframe1”);
iframe2.html

<script>
window.parent.printWhoCalledMe();
</script>
<script>
window.parent.printWhoCalledMe("iframe2");
</script>

window.parent.printWhoCalledMe(“iframe2”);

如果使用
apply
调用父函数,则可以将上下文更改为框架
窗口

window.parent.printWhoCalledMe.apply(this);

function printWhoCalledMe() {
  console.log(this); // this now refers to the frame window
}

很黑,但你可以用这样的东西:

  • 使用
    caller
    获取调用所需函数的函数的引用
  • 继续使用
    Object.getPrototypeOf
    ,直到到达该领域的
    Object.prototype
  • 迭代所有帧窗口,并比较
    对象。prototype
  • 找到匹配项后,使用
    frameElement
    获取iframe
  • 这需要sameorigin、无沙箱和草率模式。例如:

    window.func = function func() {
      var proto, nextProto = func.caller;
      while (nextProto) {
        proto = nextProto;
        nextProto = Object.getPrototypeOf(proto);
      }
      var win = [].find.call(window.frames, function(win) {
        try {
          return win.Object.prototype === proto;
        } catch(err) {
          return false;
        }
      });
      if (win) {
        var iframe = win.frameElement;
        console.log("function called from frame " + iframe.name);
      }
    };
    var iframe = document.createElement('iframe');
    iframe.name = "myframe";
    document.body.appendChild(iframe);
    var doc = iframe.contentDocument;
    var script = doc.createElement('script');
    script.text = "(function f(){parent.func()})()";
    doc.body.appendChild(script);
    // Logs "function called from frame myframe"
    

    调用父函数时,如果不将此信息包含在参数中,则无法获取此信息

    幸运的是,这相当容易。假设给每个iframe一个id,只需将iframe的id传递给正在调用的函数。您可以像下面这样获取iframe的id:
    window.frameElement.id

    例如:

    iframe1.html

    <script>
    window.parent.printWhoCalledMe();
    </script>
    
    <script>
    window.parent.printWhoCalledMe("iframe1");
    </script>
    
    <script>
        window.parent.printWhoCalledMe(window.frameElement.id);
    </script>
    
    
    window.parent.printWhoCalledMe(window.frameElement.id);
    
    parent.html

    <script>
    function printWhoCalledMe() {
      console.log(???);  // what goes here that will identify the caller?
    }
    <iframe src="iframe1.html"></iframe>
    <iframe src="iframe2.html"></iframe>
    
    <script>
        function printWhoCalledMe(iframeId) {
            console.log(iframeId);  // Prints the id of the iframe that called the function
        }
    </script>
    
    
    函数printWhoCallerDME(iframeId){
    log(iframeId);//打印调用函数的iframe的id
    }
    
    可能值得通过
    arguments.callee.caller
    来研究一些东西,但我无法确定函数的窗口对象是否可以通过该路径使用。