2

これが例です

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>

iframe1.html

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

iframe2.html

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

より大きな質問は、iframeで一度に1つずつ一連のテストを実行するテストハーネスがあることです。各テスト呼び出しwindow.parent.reportOnTest(success)

1つ以上のiframeでテストを実行して並列化することを検討していますが、すべてのテスト、現在は1000のテストを実行し、呼び出しをからそのwindow.parent.reportOnTest(success)ようなものに変更する必要がありwindow.parent.reportOnTest(success, window.location.href)ます。

テストを変更せずに、どのテストが親を呼び出しているかを把握する方法があるかどうか疑問に思っています。

注:試してみました

function printWhoCalledMe() {
  console.log(window.location.href);
}

しかし、それは親のhrefを出力します。

4

4 に答える 4

0

そのような文字列値を使用しなければならないのではないかと心配しています。

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

そして、そのような引数を使用して、子フレームからこの関数を呼び出すことができます。

iframe1.html

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

iframe2.html

<script>
window.parent.printWhoCalledMe("iframe2");
</script>
于 2012-11-27T10:27:51.913 に答える
0

を使用して親関数を呼び出すとapply、コンテキストをフレームに変更できますwindow

window.parent.printWhoCalledMe.apply(this);

function printWhoCalledMe() {
  console.log(this); // this now refers to the frame window
}
于 2012-11-27T10:29:00.220 に答える