有没有办法找出哪个iframe正在调用父对象';s JavaScript函数
这里有一个例子 parent.html有没有办法找出哪个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>
<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
获取iframewindow.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
来研究一些东西,但我无法确定函数的窗口对象是否可以通过该路径使用。